Instead of using the /regex/g
syntax, you can construct a new RegExp object:
var replace = "regex";
var re = new RegExp(replace,"g");
You can dynamically create regex objects this way. Then you will do:
"mystring".replace(re, "newstring");
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…