我试图为原型的String对象有一个replaceWith功能,使我能够的情况下直接使用正则表达式替换替换双层大括号用JavaScript
String.prototype.replaceWith = function(f, t) {
var regExp = new RegExp("[" + f + "]", "g");
return this.replace(regExp, t);
};
当我测试我的代码在此字符串{{你好}}例如我发现,更换双大括号是一个问题
测试
'{{Hello}}'.replaceWith('{{','<<').replaceWith('}}','>>');
结果
"<<<<Hello>>>>"
当它应该是
"<<Hello>>"
有什么不对我的脚本?
感谢您的帮助
看看你是否可以找到许多好的模板引擎现在可用的信息。 –