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

查看:24
本文介绍了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天全站免登陆