?
。这将检查前一个元素中的零个或一个。您可以将此符号视为前一个元素是可选的。例如,美式英语和英式英语略有不同,您可以使用问号来匹配两种拼写。 让美国人=“颜色”;
让british =“color”;
让rainbowRegex = / colou?r /;
rainbowRegex.test(美国); //返回true
rainbowRegex.test(英国); //返回true
favRegex
以匹配该单词的美国英语(收藏)和英国英语(收藏)版本。 ?
。
testString: 'assert(favRegex.source.match(/\?/).length > 0, "Your regex should use the optional symbol, ?
.");'
- text: 你的正则表达式应该匹配"favorite"
testString: 'assert(favRegex.test("favorite"), "Your regex should match "favorite"
");'
- text: 你的正则表达式应该匹配"favourite"
testString: 'assert(favRegex.test("favourite"), "Your regex should match "favourite"
");'
- text: 你的正则表达式不应该匹配"fav"
testString: 'assert(!favRegex.test("fav"), "Your regex should not match "fav"
");'
```