JavaScript .replace不会替换所有出现的事件 [英] JavaScript .replace doesn't replace all occurrences
问题描述
可能重复:
:要执行全局搜索和替换,请在其中包含g开关正则表达式,或者如果第一个参数是字符串,则包含g在flags参数中.注意:flags参数在v8 Core(Chrome和Node.js)中不起作用,将从Firefox中删除.
应该是:
"11.111.11" .replace(/\./g,'');
此版本(在编辑时)确实可以在Firefox中使用...
"11.111.11" .replace('.','','g');
...但是,正如在MDN页面上所指出的那样,其支持将很快消失.
Possible Duplicate:
Javascript multiple replace
How do I replace all occurrences of "/" in a string with "_" in JavaScript?In JavaScript,
"11.111.11".replace(".", "")
results in"11111.11"
. How can that be?Firebug Screenshot:
解决方案Quote from the doc:
To perform a global search and replace, either include the g switch in the regular expression or if the first parameter is a string, include g in the flags parameter. Note: The flags argument does not work in v8 Core (Chrome and Node.js) and will be removed from Firefox.
So it should be:
"11.111.11".replace(/\./g, '');
This version (at the moment of edit) does work in Firefox...
"11.111.11".replace('.', '', 'g');
... but, as noted at the very MDN page, its support will be dropped soon.
这篇关于JavaScript .replace不会替换所有出现的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!