Randell Dawson 05f73ca409 fix(curriculum): Convert blockquote elements to triple backtick syntax for JavaScript Algorithms and Data Structures (#35992)
* fix: convert js algorithms and data structures

* fix: revert some blocks back to blockquote

* fix: reverted comparison code block to blockquotes

* fix: change js to json

Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* fix: convert various section to triple backticks

* fix: Make the formatting consistent for comparisons
2019-05-17 08:20:30 -05:00

3.6 KiB

id, title, challengeType
id title challengeType
587d7dbb367417b2b2512baa Reuse Patterns Using Capture Groups 1

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.

Tests

tests:
  - text: Your regex should use the shorthand character class for digits.
    testString: assert(reRegex.source.match(/\\d/), 'Your regex should use the shorthand character class for digits.');
  - text: Your regex should reuse the capture group twice.
    testString: assert(reRegex.source.match(/\\\d/g).length === 2, 'Your regex should reuse the capture group twice.');
  - text: Your regex should have two spaces separating the three numbers.
    testString: assert(reRegex.source.match(/ |\\s/g).length === 2, 'Your regex should have two spaces separating the three numbers.');
  - text: Your regex should match <code>"42 42 42"</code>.
    testString: assert(reRegex.test("42 42 42"), 'Your regex should match <code>"42 42 42"</code>.');
  - text: Your regex should match <code>"100 100 100"</code>.
    testString: assert(reRegex.test("100 100 100"), 'Your regex should match <code>"100 100 100"</code>.');
  - text: Your regex should not match <code>"42 42 42 42"</code>.
    testString: assert.equal(("42 42 42 42").match(reRegex.source), null, 'Your regex should not match <code>"42 42 42 42"</code>.');
  - text: Your regex should not match <code>"42 42"</code>.
    testString: assert.equal(("42 42").match(reRegex.source), null, 'Your regex should not match <code>"42 42"</code>.');
  - text: Your regex should not match <code>"101 102 103"</code>.
    testString: assert(!reRegex.test("101 102 103"), 'Your regex should not match <code>"101 102 103"</code>.');
  - text: Your regex should not match <code>"1 2 3"</code>.
    testString: assert(!reRegex.test("1 2 3"), 'Your regex should not match <code>"1 2 3"</code>.');
  - text: Your regex should match <code>"10 10 10"</code>.
    testString: assert(reRegex.test("10 10 10"), 'Your regex should match <code>"10 10 10"</code>.');

Challenge Seed

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

Solution

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