.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
check-for-all-or-none.english.md
check-for-mixed-grouping-of-characters.english.md
extract-matches.english.md
find-characters-with-lazy-matching.english.md
find-more-than-the-first-match.english.md
find-one-or-more-criminals-in-a-hunt.english.md
ignore-case-while-matching.english.md
match-a-literal-string-with-different-possibilities.english.md
match-all-letters-and-numbers.english.md
match-all-non-numbers.english.md
match-all-numbers.english.md
match-anything-with-wildcard-period.english.md
match-beginning-string-patterns.english.md
match-characters-that-occur-one-or-more-times.english.md
match-characters-that-occur-zero-or-more-times.english.md
match-ending-string-patterns.english.md
match-everything-but-letters-and-numbers.english.md
match-letters-of-the-alphabet.english.md
match-literal-strings.english.md
match-non-whitespace-characters.english.md
match-numbers-and-letters-of-the-alphabet.english.md
match-single-character-with-multiple-possibilities.english.md
match-single-characters-not-specified.english.md
match-whitespace.english.md
positive-and-negative-lookahead.english.md
remove-whitespace-from-start-and-end.english.md
restrict-possible-usernames.english.md
reuse-patterns-using-capture-groups.english.md
specify-exact-number-of-matches.english.md
specify-only-the-lower-number-of-matches.english.md
specify-upper-and-lower-number-of-matches.english.md
use-capture-groups-to-search-and-replace.english.md
using-the-test-method.english.md
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
portuguese
russian
spanish
formattingConversion
math-challenges
requiresTests
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
lib.js
md-translation.js
package-entry.js
package-lock.json
package.json
utils.js
docs
guide
mock-guide
search-indexing
tools
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
LICENSE.md
README.french.md
README.italian.md
README.korean.md
README.md
SECURITY.md
azure-pipelines.yml
change_volumes_owner.sh
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
lerna.json
libcimp_index_js.patch
package-lock.json
package.json
patch_npm_and_install.sh
sample.env
85 lines
3.1 KiB
Markdown
85 lines
3.1 KiB
Markdown
---
|
|
id: 587d7dbb367417b2b2512baa
|
|
title: Reuse Patterns Using Capture Groups
|
|
challengeType: 1
|
|
---
|
|
|
|
## Description
|
|
<section id='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 <code>capture groups</code>. Parentheses, <code>(</code> and <code>)</code>, 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 (<code>\</code>) and then a number. This number starts at 1 and increases with each additional capture group you use. An example would be <code>\1</code> to match the first group.
|
|
The example below matches any word that occurs twice separated by a space:
|
|
|
|
```js
|
|
let repeatStr = "regex regex";
|
|
let repeatRegex = /(\w+)\s\1/;
|
|
repeatRegex.test(repeatStr); // Returns true
|
|
repeatStr.match(repeatRegex); // Returns ["regex regex", "regex"]
|
|
```
|
|
|
|
Using the <code>.match()</code> method on a string will return an array with the string it matches, along with its capture group.
|
|
</section>
|
|
|
|
## Instructions
|
|
<section id='instructions'>
|
|
Use <code>capture groups</code> in <code>reRegex</code> to match numbers that are repeated only three times in a string, each separated by a space.
|
|
</section>
|
|
|
|
## Tests
|
|
<section id='tests'>
|
|
|
|
```yml
|
|
tests:
|
|
- text: Your regex should use the shorthand character class for digits.
|
|
testString: assert(reRegex.source.match(/\\d/));
|
|
- text: Your regex should reuse a capture group twice.
|
|
testString: assert(reRegex.source.match(/\\1|\\2/g).length >= 2);
|
|
- text: Your regex should have two spaces separating the three numbers.
|
|
testString: assert(reRegex.source.match(/ |\\s/g).length === 2 || reRegex.source.match(/\(\\s\)(?=.*\\(1|2))/g));
|
|
- text: Your regex should match <code>"42 42 42"</code>.
|
|
testString: assert(reRegex.test("42 42 42"));
|
|
- text: Your regex should match <code>"100 100 100"</code>.
|
|
testString: assert(reRegex.test("100 100 100"));
|
|
- text: Your regex should not match <code>"42 42 42 42"</code>.
|
|
testString: assert.equal(("42 42 42 42").match(reRegex.source), null);
|
|
- text: Your regex should not match <code>"42 42"</code>.
|
|
testString: assert.equal(("42 42").match(reRegex.source), null);
|
|
- text: Your regex should not match <code>"101 102 103"</code>.
|
|
testString: assert(!reRegex.test("101 102 103"));
|
|
- text: Your regex should not match <code>"1 2 3"</code>.
|
|
testString: assert(!reRegex.test("1 2 3"));
|
|
- text: Your regex should match <code>"10 10 10"</code>.
|
|
testString: assert(reRegex.test("10 10 10"));
|
|
|
|
```
|
|
|
|
</section>
|
|
|
|
## Challenge Seed
|
|
<section id='challengeSeed'>
|
|
|
|
<div id='js-seed'>
|
|
|
|
```js
|
|
let repeatNum = "42 42 42";
|
|
let reRegex = /change/; // Change this line
|
|
let result = reRegex.test(repeatNum);
|
|
```
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
## Solution
|
|
<section id='solution'>
|
|
|
|
```js
|
|
let repeatNum = "42 42 42";
|
|
let reRegex = /^(\d+)\s\1\s\1$/;
|
|
let result = reRegex.test(repeatNum);
|
|
```
|
|
</section>
|