Javascript 从字符串中删除点符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10584438/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:58:53  来源:igfitidea点击:

removing dot symbol from a string

javascript

提问by user1371896

Possible Duplicate:
How to replace all points in a string in JavaScript

可能的重复:
如何在 JavaScript 中替换字符串中的所有点

I am trying to remove '.'(dot) symbol from my string. and The code that Ive used is

我试图从我的字符串中删除 '.'(点)符号。我使用的代码是

checkedNew = checked.replace('.', "");

Bt when I try to alert the value of checkedNew, for example if the checkedNew has original value U.S. Marshal, the output that I get is US. Marshal, it will not remove the second dot in that string. How do remove all dot symbols?

Bt 当我尝试提醒checkedNew 的值时,例如如果checkedNew 具有原始值US Marshal,我得到的输出是US。元帅,它不会删除该字符串中的第二个点。如何删除所有点符号?

回答by Elliot Bonneville

Split the string on all the .'s and then join it again with empty spaces, like this:

在所有的.'s上拆分字符串,然后用空格再次连接它,如下所示:

checkedNew = checked.split('.').join("");

回答by Quentin

You need to perform a global replacement as, by default, replaceonly performs one replacement. In theory you can pass an instruction to be global as the third argument, but that has some compatibility issues. Use a regular expression instead.

您需要执行全局替换,因为默认情况下replace只执行一次替换。理论上,您可以将全局指令作为第三个参数传递,但这存在一些兼容性问题。请改用正则表达式。

checkedNew = checked.replace(/\./g, "");

回答by jbabey

replace will only replace the first occurance. To get around this, use a regex with the global option turned on:

replace 只会替换第一次出现。要解决此问题,请使用启用了全局选项的正则表达式:

checked.replace(/\./g, '');