Using a variable as a regular expression to find all matches inside a string
Often I have to use a value from a variable and not a fixed string inside a regular expression. For example if I wanted to iterate over the english alphabet and find the number of occurrences of each letter inside a string, I had to do something like the following:
the previous example would work and would find all occurrences of “a”. However, if I wanted to use a variable to store the searched letter, then the following would not return anything, since the name and not the value of letterToMatch variable would be parsed as string:
the following would work, but would only return the first occurrence of the letter:
So what is the solution if we wanted to use the value of a variable as a regular expression and find all the occurrences of this value inside a string?
var matches = testString.match(rgxp).length; Using the RegExp Object and creating a new instance of it, we can define as second parameter the modifier that affects the matching of the first parameter. The available modifiers are the following:
- i: case-insensitive match
- g: find all matches
- m: multiline matching
You can find the code of the article here.