\s搜索空格,并使用小写s 。您还可以搜索除空格之外的所有内容。使用\S搜索非空格, \S是一个大写的s 。此模式将不匹配空格,回车符,制表符,换页符和换行符。你可以认为它类似于字符类[^ \r\t\f\n\v] 。 让whiteSpace =“空白。到处都是空白!”
让nonSpaceRegex = / \ S / g;
whiteSpace.match(nonSpaceRegex)。长度; //返回32
countNonWhiteSpace以在字符串中查找多个非空白字符。 \S/code> to match all non-whitespace characters.");'
- text: 你的正则表达式应该在"Men are from Mars and women are from Venus."找到35个非空格"Men are from Mars and women are from Venus."
testString: 'assert("Men are from Mars and women are from Venus.".match(countNonWhiteSpace).length == 35, "Your regex should find 35 non-spaces in "Men are from Mars and women are from Venus."");'
- text: '你的正则表达式应该在"Space: the final frontier."找到23个非空格"Space: the final frontier."'
testString: 'assert("Space: the final frontier.".match(countNonWhiteSpace).length == 23, "Your regex should find 23 non-spaces in "Space: the final frontier."");'
- text: 你的正则表达式应该在"MindYourPersonalSpace"找到21个非空格
testString: 'assert("MindYourPersonalSpace".match(countNonWhiteSpace).length == 21, "Your regex should find 21 non-spaces in "MindYourPersonalSpace"");'
```