Regex: Select Everything Else
I would like to select everything what's not [a-z0-9 ]
So if the string is:
Hello! How are you Mr. 007?
Then the result wo开发者_运维百科uld be: !,?,.
Use a negated character class:
[^a-z0-9]
You will also need a case-insensitive modifier, or alternatively use this: [^a-zA-Z0-9]
.
Note that your expected result is incorrect: you missed the space character. If you want to also not match the space character you need to include it in the character class: [^a-zA-Z0-9 ]
.
With ^
at the beginning of a character class, you negate that class:
str.match(/[^a-z0-9 ]/gi)
You should also add space (or \s
(which is space, tab,...)) and the modifier i
to make the match case insensitive.
Read more about regular expressions in JavaScript and regular expressions in general.
精彩评论