为了满足我在正则表达式中插入变量/别名/函数的需要,我想到了以下方法:
oldre = /xx\(""\)/;
function newre(e){
return RegExp(e.toString().replace(/\//g,"").replace(/xx/g, yy), "g")
};
String.prototype.replaceAll = this.replace(newre(oldre), "withThis");
其中“oldre”是我要插入变量的原始regexp,
“xx”是该变量/别名/函数的占位符,
“yy”是实际的变量名、别名或函数。