\s
搜寻空格,其中s
是小写。此匹配模式不仅匹配空格,还匹配回车符、制表符、换页符和换行符,可以将其视为与[\r\t\f\n\v]
类似。
```js
let whiteSpace = "Whitespace. Whitespace everywhere!"
let spaceRegex = /\s/g;
whiteSpace.match(spaceRegex);
// Returns [" ", " "]
```
countWhiteSpace
查找字符串中的多个空白字符。
\s
匹配所有的空白。
testString: assert(/\\s/.test(countWhiteSpace.source));
- text: "你的正则表达式应该在'Men are from Mars and women are from Venus.'
中匹配到 8 个空白字符。"
testString: assert("Men are from Mars and women are from Venus.".match(countWhiteSpace).length == 8);
- text: '你的正则表达式应该在"Space: the final frontier."
中匹配到 3 个空白字符。'
testString: 'assert("Space: the final frontier.".match(countWhiteSpace).length == 3);'
- text: "你的正则表达式在'MindYourPersonalSpace'
中应该匹配不到空白字符。"
testString: assert("MindYourPersonalSpace".match(countWhiteSpace) == null);
```