【发布时间】:2010-09-13 15:18:29
【问题描述】:
我正在尝试替换某些 html 字符串中括号中的字符串。当我使用常规替换时,它很快,当我尝试为全局替换创建模式时,它最终会引发堆栈溢出错误。似乎在流程路径的某个地方,它将我的单个字符串转换为字符数组。 有什么想法吗?
var o = { bob : 'is cool', steve : 'is not' };
for (n in o) {
/*
pattern = new RegExp('\[' + n.toUpperCase() + '\]', 'g');
retString = retString.replace(pattern, o[n].toString());
*/
retString = retString.replace('[' + n.toUpperCase() + ']', o[n].toString());
}
【问题讨论】:
-
你的
o变量是什么样的? -
{ bob : '很酷', steve : '不是' } 等等...
标签: javascript regex performance