freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../regular-expressions/match-single-character-with...

3.1 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7db5367417b2b2512b95 Match Single Character with Multiple Possibilities 1 将单个角色与多种可能性相匹配

Description

您学习了如何匹配文字模式( /literal/ )和通配符( /./ )。这些是正则表达式的极端,其中一个找到完全匹配,另一个匹配一切。有两个极端之间可以平衡的选项。您可以使用character classes搜索具有一定灵活性的文字模式。字符类允许您通过将它们放在方括号( [] )括号内来定义要匹配的一组字符。例如,您想匹配"bag" "big""bug"但不匹配"bog" 。您可以创建regex /b[aiu]g/来执行此操作。 [aiu]是仅匹配字符"a" "i""u"的字符类。
让bigStr =“大”;
让bagStr =“bag”;
让bugStr =“bug”;
让bogStr =“bog”;
让bgRegex = / b [aiu] g /;
bigStr.matchbgRegex; //返回[“大”]
bagStr.matchbgRegex; //返回[“bag”]
bugStr.matchbgRegex; //返回[“bug”]
bogStr.matchbgRegex; //返回null

Instructions

在正则表达式vowelRegex使用带元音( a e i o u )的字符类来查找字符串quoteSample中的所有元音。 注意
确保匹配大写和小写元音。

Tests

tests:
  - text: 你应该找到所有25个元音。
    testString: 'assert(result.length == 25, "You should find all 25 vowels.");'
  - text: 你的正则表达式<code>vowelRegex</code>应该使用一个字符类。
    testString: 'assert(/\[.*\]/.test(vowelRegex.source), "Your regex <code>vowelRegex</code> should use a character class.");'
  - text: 你的正则表达式<code>vowelRegex</code>应该使用全局标志。
    testString: 'assert(vowelRegex.flags.match(/g/).length == 1, "Your regex <code>vowelRegex</code> should use the global flag.");'
  - text: 你的正则表达式<code>vowelRegex</code>应该使用不区分大小写的标志。
    testString: 'assert(vowelRegex.flags.match(/i/).length == 1, "Your regex <code>vowelRegex</code> should use the case insensitive flag.");'
  - text: 你的正则表达式不应该与任何辅音匹配。
    testString: 'assert(!/[b-df-hj-np-tv-z]/gi.test(result.join()), "Your regex should not match any consonants.");'

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

// solution required