javascript use variable regex

JavaScript
// CREATE A REGEXP object
var replace = "regex";
var re = new RegExp(replace,"g");
//then use
"mystring".replace(re, "newstring")
Source

Also in JavaScript: