Nicholas Carrigan (he/him) 8d8d25e9f2
fix(learn): address escaped backticks (#40717)
* fix(learn): address escaped backticks

Addresses the instances of escaped backticks - where a backtick is
preceded by a backslash.  In most cases, this was left over from the
old parser. In some cases, a backtick was intended to be wrapped in
code tags and has been adjusted accordingly.

This issue came to light due to a bug in the translation flow on
Crowdin.

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

* fix: EVEN MORE :( :( :(

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

* fix: backslash nightmares

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

* fix: When you wish upon a *******

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

* fix(curriculum): md error introduced by formatter

* fix(curriculum): remove extra `s

* fix: restore quote symbol

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

* fix: Typo

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* fix: apply review changes

Applying review feedback from call with @RandellDawson.

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

* fix: markdown does weird stuff sometimes

Can't stick backticks together - use code.

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

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2021-01-20 19:01:00 -07:00

2.7 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7dbb367417b2b2512baa Reuse Patterns Using Capture Groups 1 301364 reuse-patterns-using-capture-groups

--description--

Some patterns you search for will occur multiple times in a string. It is wasteful to manually repeat that regex. There is a better way to specify when you have multiple repeat substrings in your string.

You can search for repeat substrings using capture groups. Parentheses, ( and ), are used to find repeat substrings. You put the regex of the pattern that will repeat in between the parentheses.

To specify where that repeat string will appear, you use a backslash (\) and then a number. This number starts at 1 and increases with each additional capture group you use. An example would be \1 to match the first group.

The example below matches any word that occurs twice separated by a space:

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

Using the .match() method on a string will return an array with the string it matches, along with its capture group.

--instructions--

Use capture groups in reRegex to match numbers that are repeated only three times in a string, each separated by a space.

--hints--

Your regex should use the shorthand character class for digits.

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

Your regex should reuse a capture group twice.

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

Your regex should have two spaces separating the three numbers.

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

Your regex should match "42 42 42".

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

Your regex should match "100 100 100".

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

Your regex should not match "42 42 42 42".

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

Your regex should not match "42 42".

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

Your regex should not match "101 102 103".

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

Your regex should not match "1 2 3".

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

Your regex should match "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);