According to this site the following replace method should work, though I am sceptical. http://www.bennadel.com/blog/55-Using-Methods-in-Javascript-Replace-Method.htm
I think you're looking for new RegExp(pattern, modifiers).
http://www.w3schools.com/jsref/jsref_obj_regexp.asp