/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.match(bgRegex); //返回[“大”]
bagStr.match(bgRegex); //返回[“bag”]
bugStr.match(bgRegex); //返回[“bug”]
bogStr.match(bgRegex); //返回null
vowelRegex使用带元音( a , e , i , o , u )的字符类来查找字符串quoteSample中的所有元音。 注意 vowelRegex应该使用一个字符类。
testString: 'assert(/\[.*\]/.test(vowelRegex.source), "Your regex vowelRegex should use a character class.");'
- text: 你的正则表达式vowelRegex应该使用全局标志。
testString: 'assert(vowelRegex.flags.match(/g/).length == 1, "Your regex vowelRegex should use the global flag.");'
- text: 你的正则表达式vowelRegex应该使用不区分大小写的标志。
testString: 'assert(vowelRegex.flags.match(/i/).length == 1, "Your regex vowelRegex 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.");'
```