如何删除所有不是字母或数字的字符?
我有一根绳子:
var string = 'This - is my 4 String, and i - want remove all characters 49494 that are not letters or "numbers"';我想转化成这样:
var string = 'This is my 4 String and i want remove all characters 49494 that are not letters or numbers'这有可能吗?
谢谢!!
发布于 2015-06-14 00:36:58
您可以使用这样的正则表达式:
[\W_]+这样做的目的是与\W匹配一个非单词字符(那些不是A-Z、a-z、0-9和_的字符),并添加显式_ (因为下划线被认为是单词字符)。
var str = 's - is my 4 String, and i - want remove all characters 49494 that are not letters or "numbers"';
var result = str.replace(/[\W_]+/g, ' ');发布于 2015-06-13 23:27:56
我喜欢这样做,是使用RegEx。这将选择所有非-letters和non-numbers,并将它们替换为nothing或删除它们:
string = string.replace(/[^\s\dA-Z]/gi, '').replace(/ +/g, ' ');表外:
[^ NOT any of there
\s space
\d digit
A-Z letter
]发布于 2015-06-13 23:28:39
是的,使用正则表达式是可能的
string = string.replace(/[^a-z0-9]+|\s+/gmi, " ");https://stackoverflow.com/questions/30824525
复制相似问题