views:

22

answers:

1
var foo = "blaa";
var regex = /foo/i;

results in /foo/i instead of /blaa/i.

+5  A: 

You can use the RegExp constructor:

var regex = new RegExp(foo, 'i');

It takes two arguments, the first expect a string that represents the regular expression pattern and in the second optional argument you can define the regular expression flags you want.

CMS