简体   繁体   English

JavaScript从字符串中删除fullstop,逗号和空格

[英]JavaScript remove fullstop, comma and spaces from a string

I'm trying to remove the fullstops, commas and spaces from a string. 我正在尝试从字符串中删除fullstops,逗号和空格。 I'm pretty sure I have to do this using this: str.replace(<some Regex here>, ""); 我很确定我必须这样做: str.replace(<some Regex here>, "");

I'm just not very familiar with Regex, how can I accomplish this? 我对Regex不是很熟悉,我怎么能做到这一点?

Use this regex /[.,\\s]/g 使用此正则表达式/ [。, /[.,\\s]/g

 var str = 'abc abc a, .aa '; var regex = /[.,\\s]/g; var result = str.replace(regex, ''); document.write(result); 

You don't need to escape character except ^-]\\ in character class [] 除了字符类[] ^-]\\之外,您不需要转义字符

Any character except ^-]\\ add that character to the possible matches for the character class. 除^ - ] \\之外的任何字符都将该字符添加到字符类的可能匹配项中。

我相信这应该做到:

str.replace(/[.,\s]/g, '');

像这样的东西应该工作......

str=str.replace(/./g,'').replace(/,/g,'').replace(/ /g,'');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM