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


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

var foo = 'foo';

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

To do it properly, do the following instead:

var filter = new RegExp('foo');

Leave a Reply

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