Javascript正则表达式模式\ W是否包含空格?

时间:2011-05-28 17:22:29

标签: javascript regex

我使用这个表达式:/\W+/g来匹配所有不是数字,字母和空格的字符。它似乎包括空间。我如何构建一个不包含空格的正则表达式?

3 个答案:

答案 0 :(得分:1)

/[^a-z0-9\s]+/ig

说明:

[^   Character class which matches characters NOT in the following class
a-z  All lowercase letters of the alphabet
0-9  All numbers
\s   Whitespace characters
]    End of the character class

i   Case-insensitivity to match uppercase letters

答案 1 :(得分:1)

\W更准确的措辞是任何非字母数字字符\s适用于任意空白

所以,它会是这样的: [^ \ s]

答案 2 :(得分:0)

\W表示“非单词字符”,即\w的倒数,因此它也会匹配空格。不过,我有点惊讶它不符合数字。