how can i remove all characters that are not letters or 'numbers'??
I have a string:
var string = 'This - is my 4 String, and i - want remove all characters 49494 that are not letters or "numbers"';
And i want transform into this:
var string = 'This is my 4 String and i want remove all characters 49494 that are not letters or numbers'
This is possible?
Thanks!!
You can use a regex like this:
[\W_]+
The idea is to match with \\W
a non word character (those characters that are not AZ
, az
, 0-9
and _
) and also add explicitly _
(since underscore is considered a word character)
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, ' ');
The way I like to do it is using a RegEx. This will select all non -letters and non -numbers and replace them with nothing or deleting them:
string = string.replace(/[^\s\dA-Z]/gi, '').replace(/ +/g, ' ');
Explanantion:
[^ NOT any of there
\s space
\d digit
A-Z letter
]
是的,可以使用正则表达式
string = string.replace(/[^a-z0-9]+|\s+/gmi, " ");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.