如何使用正则表达式从JavaScript中删除字符串中的所有标点符号?

如果我有一个包含任何类型的非字母数字字符的字符串:


"This., -/ is #! an $ % ^ & * example ;: {} of a = -_ string with `~)() punctuation"

我如何在JavaScript中获得它的无标点版本:


"This is an example of a string with punctuation"


慕沐林林
浏览 1597回答 3
3回答

噜噜哒

如果要从字符串中删除特定标点符号,最好明确删除您想要的内容replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"")执行上述操作仍然不会返回指定字符串的字符串。如果你想删除因删除疯狂标点符号而留下的任何额外空格,那么你将要做类似的事情replace(/\s{2,}/g," ");我的完整例子:var s = "This., -/ is #! an $ % ^ & * example ;: {} of a = -_ string with `~)() punctuation";var punctuationless = s.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");var finalString = punctuationless.replace(/\s{2,}/g," ");
打开App,查看更多内容
随时随地看视频慕课网APP