2.3 KiB
2.3 KiB
id, challengeType, forumTopicId, title
id | challengeType | forumTopicId | title |
---|---|---|---|
587d7db9367417b2b2512ba4 | 1 | 18210 | 匹配非空白字符 |
Description
s
的缩写\s
来搜寻空白字符。还可以搜寻除了空格之外的所有内容。
使用\S
搜寻非空白字符,其中S
是大写。此匹配模式将不匹配空格、回车符、制表符、换页符和换行符。可以认为这类似于元字符[^\r\t\f\n\v]
。
let whiteSpace = "Whitespace. Whitespace everywhere!"
let nonSpaceRegex = /\S/g;
whiteSpace.match(nonSpaceRegex).length; // Returns 32
Instructions
countNonWhiteSpace
以查找字符串中的多个非空字符。
Tests
tests:
- text: 你的正则表达式应该使用全局状态修正符。
testString: assert(countNonWhiteSpace.global);
- text: 正则表达式应该使用元字符 <code>\S/code> 来匹配所有的非空格字符。
testString: assert(/\\S/.test(countNonWhiteSpace.source));
- text: "你的正则表达式应该在<code>'Men are from Mars and women are from Venus.'</code>中匹配到 35 个非空白字符。"
testString: assert("Men are from Mars and women are from Venus.".match(countNonWhiteSpace).length == 35);
- text: '你的正则表达式应该在<code>"Space: the final frontier."</code>中匹配到 23 个非空白字符。'
testString: 'assert("Space: the final frontier.".match(countNonWhiteSpace).length == 23);'
- text: "你的正则表达式应该在<code>'MindYourPersonalSpace'</code>中匹配到 21 个非空白字符。"
testString: assert("MindYourPersonalSpace".match(countNonWhiteSpace).length == 21);
Challenge Seed
let sample = "Whitespace is important in separating words";
let countNonWhiteSpace = /change/; // Change this line
let result = sample.match(countNonWhiteSpace);
Solution
let sample = "Whitespace is important in separating words";
let countNonWhiteSpace = /\S/g; // Change this line
let result = sample.match(countNonWhiteSpace);