2018-10-04 14:37:37 +01:00
---
id: 587d7dbb367417b2b2512baa
title: Reuse Patterns Using Capture Groups
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301364
2021-01-13 03:31:00 +01:00
dashedName: reuse-patterns-using-capture-groups
2018-10-04 14:37:37 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-10-04 14:37:37 +01:00
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.
2020-11-27 19:02:05 +01:00
You can search for repeat substrings using < dfn > capture groups</ dfn > . 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.
2018-10-04 14:37:37 +01:00
The example below matches any word that occurs twice separated by a space:
2019-05-17 06:20:30 -07:00
```js
let repeatStr = "regex regex";
let repeatRegex = /(\w+)\s\1/;
repeatRegex.test(repeatStr); // Returns true
repeatStr.match(repeatRegex); // Returns ["regex regex", "regex"]
```
2020-11-27 19:02:05 +01:00
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.
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
Your regex should use the shorthand character class for digits.
```js
assert(reRegex.source.match(/\\d/));
2018-10-04 14:37:37 +01:00
```
2020-11-27 19:02:05 +01:00
Your regex should reuse a capture group twice.
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(reRegex.source.match(/\\1|\\2/g).length >= 2);
```
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
Your regex should have two spaces separating the three numbers.
2018-10-04 14:37:37 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(
reRegex.source.match(/ |\\s/g).length === 2 ||
reRegex.source.match(/\(\\s\)(?=.*\\(1|2))/g)
);
```
Your regex should match `"42 42 42"` .
```js
assert(reRegex.test('42 42 42'));
2018-10-04 14:37:37 +01:00
```
2020-11-27 19:02:05 +01:00
Your regex should match `"100 100 100"` .
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(reRegex.test('100 100 100'));
```
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
Your regex should not match `"42 42 42 42"` .
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.equal('42 42 42 42'.match(reRegex.source), null);
```
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
Your regex should not match `"42 42"` .
```js
assert.equal('42 42'.match(reRegex.source), null);
```
Your regex should not match `"101 102 103"` .
```js
assert(!reRegex.test('101 102 103'));
```
Your regex should not match `"1 2 3"` .
```js
assert(!reRegex.test('1 2 3'));
```
Your regex should match `"10 10 10"` .
```js
assert(reRegex.test('10 10 10'));
```
# --seed--
## --seed-contents--
2018-10-04 14:37:37 +01:00
```js
2019-02-17 06:10:03 +03:00
let repeatNum = "42 42 42";
2020-11-27 19:02:05 +01:00
let reRegex = /change/; // Change this line
2019-02-17 06:10:03 +03:00
let result = reRegex.test(repeatNum);
2018-10-04 14:37:37 +01:00
```
2019-07-18 08:24:12 -07:00
2020-11-27 19:02:05 +01:00
# --solutions--
```js
let repeatNum = "42 42 42";
let reRegex = /^(\d+)\s\1\s\1$/;
let result = reRegex.test(repeatNum);
```