var foo = "blaa"; var regex = /foo/i;
results in /foo/i instead of /blaa/i.
/foo/i
/blaa/i
You can use the RegExp constructor:
RegExp
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.