2.9 KiB
2.9 KiB
id, title, challengeType, forumTopicId, localeTitle
id | title | challengeType | forumTopicId | localeTitle |
---|---|---|---|---|
587d7db5367417b2b2512b95 | Match Single Character with Multiple Possibilities | 1 | 301357 | 将单个字符与多种可能性匹配 |
Description
/literal/
)和通配符(/./
)。这是正则表达式的两种极端情况,一种是精确匹配,而另一种则是匹配所有。在这两种极端情况之间有一个平衡选项。
可以使用字符集
搜寻具有一定灵活性的文字匹配模式。可以把字符集放在方括号([
和]
)之间来定义一组需要匹配的字符串。
例如,如果想要匹配"bag"
、"big"
和"bug"
,但是不想匹配"bog"
。可以创建正则表达式/b[aiu]g/
来执行此操作。[aiu]
是只匹配字符"a"
、"i"
或者"u"
的字符集。
let bigStr = "big";
let bagStr = "bag";
let bugStr = "bug";
let bogStr = "bog";
let bgRegex = /b[aiu]g/;
bigStr.match(bgRegex); // Returns ["big"]
bagStr.match(bgRegex); // Returns ["bag"]
bugStr.match(bgRegex); // Returns ["bug"]
bogStr.match(bgRegex); // Returns null
Instructions
a
、e
、i
、o
、u
)在正则表达式vowelRegex
中匹配到字符串quoteSample
中的所有元音。
注意一定要同时匹配大小写元音。
Tests
tests:
- text: 你应该匹配到所有25个元音。
testString: assert(result.length == 25);
- text: 你的正则表达式<code>vowelRegex</code>应该使用字符集。
testString: assert(/\[.*\]/.test(vowelRegex.source));
- text: 你的正则表达式<code>vowelRegex</code>应该使用全局标志。
testString: assert(vowelRegex.flags.match(/g/).length == 1);
- text: 你的正则表达式<code>vowelRegex</code>应该使用忽略大小写标志。
testString: assert(vowelRegex.flags.match(/i/).length == 1);
- text: 你的正则表达式不应该匹配任何辅音。
testString: assert(!/[b-df-hj-np-tv-z]/gi.test(result.join()));
Challenge Seed
let quoteSample = "Beware of bugs in the above code; I have only proved it correct, not tried it.";
let vowelRegex = /change/; // Change this line
let result = vowelRegex; // Change this line
Solution
let quoteSample = "Beware of bugs in the above code; I have only proved it correct, not tried it.";
let vowelRegex = /[aeiou]/gi; // Change this line
let result = quoteSample.match(vowelRegex); // Change this line