为什么 javascript 不能在 Chrome 或 IE 中替换全局标志,我该如何解决?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4981501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why doesn't the javascript replace global flag work in Chrome or IE, and how to I work around it?
提问by David R.
According to the String.prototype.replace()page on MDN, I should be able to easily replace multiple patterns just by using
根据String.prototype.replace()MDN上的页面,我应该能够通过使用轻松替换多个模式
str.replace('what to replace', 'replace with', 'flags')
and setting the flags to 'g'.
并将标志设置为'g'.
It works perfect in Firefox 3.6. But in Chrome and IE8, it only replaces the first 'what to replace'.
它在 Firefox 3.6 中完美运行。但是在 Chrome 和 IE8 中,它只替换了第一个'what to replace'.
I can use the
我可以使用
str.replace(/what to replace/gi, 'replace with')
syntax. But I'm pulling the 'what to replace'out of an array, which makes it hard to add the flags in that syntax.
句法。但是我正在'what to replace'从数组中提取,这使得很难在该语法中添加标志。
Here's the code I'm trying to use. How to I modify it to work in Chrome as well as Firefox?
这是我尝试使用的代码。如何修改它以在 Chrome 和 Firefox 中工作?
function generateQuestion()
{
//alert('variable length: '+variableList.length);
for(i=0;i<variableList.length;i++)
{
variable = variableList[i];
rep = replacementList[i];
flags = "gi";
questionText = questionText.replace(variable, rep, flags);
}
}
And why do I have to bother modifying it at all? Shouldn't Chrome evaluate the JavaScript as described in the link?
为什么我要费心修改它呢?Chrome 不应该按照链接中的描述评估 JavaScript 吗?
回答by Yi Jiang
The very page you linked to mentions:
您链接到的页面提到了:
The use of the flags parameter in the String.replace method is non-standard. For cross-browser compatibility, use a RegExp object with corresponding flags.
在 String.replace 方法中使用 flags 参数是非标准的。为了跨浏览器兼容性,请使用带有相应标志的 RegExp 对象。
Basically, it should only work on Firefox. As per the documentation, you can generate regexes dynamically using new RegExp:
基本上,它应该只适用于 Firefox。根据文档,您可以使用new RegExp以下方法动态生成正则表达式:
var regex = new RegExp(variable, 'gi');
questionText = questionText.replace(regex, rep);
This will need variableto be escaped, however.
但是,这需要variable转义。
回答by Daniel Schaffer
It appears that webkit's implementation of string.replace perhaps doesn't have the 3rd parameter, as 'foo'.replace('o','i','g')results in fiofor me.
看来 webkit 的 string.replace 实现可能没有第三个参数,这对我来说是'foo'.replace('o','i','g')结果fio。
The following appears to work however:
然而,以下似乎有效:
'foo'.replace(/o/gi,'i')
Another option is:
另一种选择是:
'foo'.replace(new RegExp('o', 'gi'),'i')
回答by typo.pl
From Mozilla Developer Network - JavaScript - String - replace
来自Mozilla 开发者网络 - JavaScript - 字符串 - 替换
Non-standard
非标
The use of the flags parameter in the String.replace method is non-standard. For cross-browser compatibility, use a RegExp object with corresponding flags.
在 String.replace 方法中使用 flags 参数是非标准的。为了跨浏览器兼容性,请使用带有相应标志的 RegExp 对象。
Working in Chrome and Firefox
在 Chrome 和 Firefox 中工作
To get your code to work in Chrome and Firefox, you'll have to create a RegExp object (since your strings aren't hardcoded) with the appropriate flags. See Mozilla Developer Network - RegExp
为了让您的代码在 Chrome 和 Firefox 中工作,您必须使用适当的标志创建一个 RegExp 对象(因为您的字符串不是硬编码的)。查看Mozilla 开发者网络 - RegExp

