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:

1
2
3
var testString = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.";
var matches = testString.match(/a/g).length;
// do this for all the remaining letters

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:

1
2
3
var letterToMatch = "a"; 
var testString = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.";
var matches = testString.match(/letterToMatch/g);
1
<div>Some <b>HTML</b></div>

the following would work, but would only return the first occurrence of the letter:

1
2
3
var letterToMatch = "a"; 
var testString = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.";
var matches = testString.match(letterToMatch);

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?

1
2
3
var letterToMatch = "a"; 
var testString = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.";
var rgxp = new RegExp(letterToMatch, "g");

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:

You can find the code of the article here.

comments powered by Disqus