Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

2.7 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7dbb367417b2b2512baa 使用捕获组重用模式 1 301364 reuse-patterns-using-capture-groups

--description--

一些你所搜寻的匹配模式会在字符串中出现多次,手动重复该正则表达式太浪费了。有一种更好的方法可以指定何时在字符串中会有多个重复的子字符串。

可以使用捕获组搜寻重复的子字符串。括号()可以用来匹配重复的子字符串。只需要把重复匹配模式的正则表达式放在括号中即可。

要指定重复字符串将出现的位置,可以使用反斜杠(\)后接一个数字。这个数字从 1 开始,随着你使用的每个捕获组的增加而增加。这里有一个示例,\1可以匹配第一个组。

下面的示例匹配任意两个被空格分割的单词:

let repeatStr = "regex regex";
let repeatRegex = /(\w+)\s\1/;
repeatRegex.test(repeatStr); // Returns true
repeatStr.match(repeatRegex); // Returns ["regex regex", "regex"]

在字符串上使用.match()方法将返回一个数组,其中包含它匹配的字符串及其捕获组。

--instructions--

在正则表达式reRegex中使用捕获组,以匹配在字符串中仅重复三次的数字,每一个都由空格分隔。

--hints--

你的正则表达式应该使用数字的速记元字符。

assert(reRegex.source.match(/\\d/));

你的正则表达式应该重用两次捕获组。

assert(reRegex.source.match(/\\1|\\2/g).length >= 2);

你的正则表达式应该有两个空格分隔这三个数字。

assert(
  reRegex.source.match(/ |\\s/g).length === 2 ||
    reRegex.source.match(/\(\\s\)(?=.*\\(1|2))/g)
);

你的正则表达式应该匹配'42 42 42'

assert(reRegex.test('42 42 42'));

你的正则表达式应该匹配'100 100 100'

assert(reRegex.test('100 100 100'));

你的正则表达式不应该匹配'42 42 42 42'

assert.equal('42 42 42 42'.match(reRegex.source), null);

你的正则表达式不应该匹配'42 42'

assert.equal('42 42'.match(reRegex.source), null);

你的正则表达式不应该匹配'101 102 103'

assert(!reRegex.test('101 102 103'));

你的正则表达式不应该匹配'1 2 3'

assert(!reRegex.test('1 2 3'));

你的正则表达式应该匹配'10 10 10'

assert(reRegex.test('10 10 10'));

--seed--

--seed-contents--

let repeatNum = "42 42 42";
let reRegex = /change/; // Change this line
let result = reRegex.test(repeatNum);

--solutions--

let repeatNum = "42 42 42";
let reRegex = /^(\d+)\s\1\s\1$/;
let result = reRegex.test(repeatNum);