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