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
removing dot symbol from a string
提问by user1371896
Possible Duplicate:
How to replace all points in a string in 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, replace
only 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, '');