We all know that to match a particular regular expression in JS, you simply do this:

"foo".match(/foo/i);

However, if you want “foo” to be a variable:

var foo = 'foo';
"foo".match(/foo/i);

This becomes confusing and I don’t think it works.

To do it properly, do the following instead:

var filter = new RegExp('foo');
"foo".match(filter);

Leave a Reply

Your email address will not be published. Required fields are marked *