JavaScript .replace不会替换所有出现的事件 [英] JavaScript .replace doesn't replace all occurrences

查看:59
本文介绍了JavaScript .replace不会替换所有出现的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
:

要执行全局搜索和替换,请在其中包含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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆