Regular expressions let us detect when a string matches a pattern. It's straightforward to make a hardcoded regular expression. This will show whether a phrase starts with a capital T:
But what if you don't know the pattern ahead of time, like in a search form? What's the syntax when you're using a variable?
I needed to find all the array elements that began with a phrase the user selected. Here's the snippet:
The important bit is the RegExp constructor on line 2. Build up the pattern like a normal string, and omit the beginning and ending slashes.
And here's how to filter the array.