我想创造一个
String.replaceAll()
方法,我认为使用正则表达式是最简洁的方法。但是,我不知道如何将变量传递给正则表达式。我已经可以这样做了,它将替换
"B"
具有
"A"
.
"ABABAB".replace(/B/g, "A");
但我想这样做:
String.prototype.replaceAll = function(replaceThis, withThis) {
this.replace(/replaceThis/g, withThis);
};
但显然这只会取代文本
"replaceThis"
…那么如何将此变量传递到regex字符串中?