\s搜索空格,这是一个小写的s 。此模式不仅匹配空格,还匹配回车符,制表符,换页符和换行符。你可以认为它类似于字符类[ \r\t\f\n\v] 。 让whiteSpace =“空白。到处都是空白!”
让spaceRegex = / \ s / g;
whiteSpace.match(spaceRegex);
//返回[“”,“”]
countWhiteSpace以查找字符串中的多个空格字符。 \s to match all whitespace characters.");'
- text: 你的正则表达式应该在"Men are from Mars and women are from Venus."找到八个空格"Men are from Mars and women are from Venus."
testString: 'assert("Men are from Mars and women are from Venus.".match(countWhiteSpace).length == 8, "Your regex should find eight spaces in "Men are from Mars and women are from Venus."");'
- text: '你的正则表达式应该在"Space: the final frontier."找到三个空格"Space: the final frontier."'
testString: 'assert("Space: the final frontier.".match(countWhiteSpace).length == 3, "Your regex should find three spaces in "Space: the final frontier."");'
- text: ''
testString: 'assert("MindYourPersonalSpace".match(countWhiteSpace) == null, "Your regex should find no spaces in "MindYourPersonalSpace"");'
```