freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../regular-expressions/check-for-all-or-none.chine...

2.0 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7dba367417b2b2512ba8 Check for All or None 1 检查全部或无

Description

有时,您要搜索的模式可能包含可能存在或可能不存在的模式。但是,尽管如此,检查它们可能很重要。您可以指定可能存在带问号的元素, ? 。这将检查前一个元素中的零个或一个。您可以将此符号视为前一个元素是可选的。例如,美式英语和英式英语略有不同,您可以使用问号来匹配两种拼写。
让美国人=“颜色”;
让british =“color”;
让rainbowRegex = / colour /;
rainbowRegex.test美国; //返回true
rainbowRegex.test英国; //返回true

Instructions

更改正则表达式favRegex以匹配该单词的美国英语(收藏)和英国英语(收藏)版本。

Tests

tests:
  - text: 你的正则表达式应该使用可选的符号, <code>?</code> 。
    testString: 'assert(favRegex.source.match(/\?/).length > 0, "Your regex should use the optional symbol, <code>?</code>.");'
  - text: 你的正则表达式应该匹配<code>&quot;favorite&quot;</code>
    testString: 'assert(favRegex.test("favorite"), "Your regex should match <code>"favorite"</code>");'
  - text: 你的正则表达式应该匹配<code>&quot;favourite&quot;</code>
    testString: 'assert(favRegex.test("favourite"), "Your regex should match <code>"favourite"</code>");'
  - text: 你的正则表达式不应该匹配<code>&quot;fav&quot;</code>
    testString: 'assert(!favRegex.test("fav"), "Your regex should not match <code>"fav"</code>");'

Challenge Seed

let favWord = "favorite";
let favRegex = /change/; // Change this line
let result = favRegex.test(favWord);

Solution

// solution required