我正在尝试原型化String对象,使其具有replaceWith函数,以便我能够直接替换而无需使用正则表达式。
String.prototype.replaceWith = function(f, t) {
var regExp = new RegExp("[" + f + "]", "g");
return this.replace(regExp, t);
};
当我在这个字符串{{Hello}}中测试我的代码时,例如,我发现替换双大括号是一个问题。
测试
'{{Hello}}'.replaceWith('{{','<<').replaceWith('}}','>>');
结果是
"<<<<Hello>>>>"
应该是
而不是
。"<<Hello>>"
我的脚本出了什么问题?谢谢你的帮助。