// Variable to a Regular Expression var re = new RegExp("regex", "g"); str.replace(re, "newstring"); // http://stackoverflow.com/questions/494035/how-do-you-pass-a-variable-to-a-regular-expression-javascript