2018-10-04 13:37:37 +00:00
---
id: 587d7dbb367417b2b2512baa
title: Reuse Patterns Using Capture Groups
challengeType: 1
2019-08-05 16:17:33 +00:00
forumTopicId: 301364
2021-01-13 02:31:00 +00:00
dashedName: reuse-patterns-using-capture-groups
2018-10-04 13:37:37 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2021-07-22 15:37:50 +00:00
Say you want to match a word that occurs multiple times like below.
2020-11-27 18:02:05 +00:00
2021-07-22 15:37:50 +00:00
```js
let repeatStr = "row row row your boat";
```
You could use `/row row row/` , but what if you don't know the specific word repeated? < dfn > Capture groups</ dfn > can be used to find repeated substrings.
Capture groups are constructed by enclosing the regex pattern to be captured in parentheses. In this case, the goal is to capture a word consisting of alphanumeric characters so the capture group will be `\w+` enclosed by parentheses: `/(\w+)/` .
2020-11-27 18:02:05 +00:00
2021-07-22 15:37:50 +00:00
The substring matched by the group is saved to a temporary "variable", which can be accessed within the same regex using a backslash and the number of the capture group (e.g. `\1` ). Capture groups are automatically numbered by the position of their opening parentheses (left to right), starting at 1.
2020-11-27 18:02:05 +00:00
2021-07-22 15:37:50 +00:00
The example below matches a word that occurs thrice separated by spaces:
2019-05-17 13:20:30 +00:00
```js
2021-07-22 15:37:50 +00:00
let repeatRegex = /(\w+) \1 \1/;
repeatRegex.test(repeatStr); // Returns true
repeatStr.match(repeatRegex); // Returns ["row row row", "row"]
2019-05-17 13:20:30 +00:00
```
2021-07-22 15:37:50 +00:00
Using the `.match()` method on a string will return an array with the matched substring, along with its captured groups.
2021-03-03 00:12:12 +00:00
2020-11-27 18:02:05 +00:00
# --instructions--
2021-01-21 03:39:23 +00:00
Use capture groups in `reRegex` to match a string that consists of only the same number repeated exactly three times separated by single spaces.
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
Your regex should use the shorthand character class for digits.
```js
assert(reRegex.source.match(/\\d/));
2018-10-04 13:37:37 +00:00
```
2020-11-27 18:02:05 +00:00
Your regex should reuse a capture group twice.
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(reRegex.source.match(/\\1|\\2/g).length >= 2);
```
2018-10-04 13:37:37 +00:00
2021-03-03 00:12:12 +00:00
Your regex should match the string `42 42 42` .
2020-11-27 18:02:05 +00:00
```js
2021-10-06 07:14:50 +00:00
reRegex.lastIndex = 0;
2020-11-27 18:02:05 +00:00
assert(reRegex.test('42 42 42'));
2018-10-04 13:37:37 +00:00
```
2021-03-03 00:12:12 +00:00
Your regex should match the string `100 100 100` .
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-10-06 07:14:50 +00:00
reRegex.lastIndex = 0;
2020-11-27 18:02:05 +00:00
assert(reRegex.test('100 100 100'));
```
2018-10-04 13:37:37 +00:00
2021-03-03 00:12:12 +00:00
Your regex should not match the string `42 42 42 42` .
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
assert.equal('42 42 42 42'.match(reRegex.source), null);
```
2018-10-04 13:37:37 +00:00
2021-03-03 00:12:12 +00:00
Your regex should not match the string `42 42` .
2020-11-27 18:02:05 +00:00
```js
assert.equal('42 42'.match(reRegex.source), null);
```
2021-03-03 00:12:12 +00:00
Your regex should not match the string `101 102 103` .
2020-11-27 18:02:05 +00:00
```js
2021-10-06 07:14:50 +00:00
reRegex.lastIndex = 0;
2020-11-27 18:02:05 +00:00
assert(!reRegex.test('101 102 103'));
```
2021-03-03 00:12:12 +00:00
Your regex should not match the string `1 2 3` .
2020-11-27 18:02:05 +00:00
```js
2021-10-06 07:14:50 +00:00
reRegex.lastIndex = 0;
2020-11-27 18:02:05 +00:00
assert(!reRegex.test('1 2 3'));
```
2021-03-03 00:12:12 +00:00
Your regex should match the string `10 10 10` .
2020-11-27 18:02:05 +00:00
```js
2021-10-06 07:14:50 +00:00
reRegex.lastIndex = 0;
2020-11-27 18:02:05 +00:00
assert(reRegex.test('10 10 10'));
```
# --seed--
## --seed-contents--
2018-10-04 13:37:37 +00:00
```js
2019-02-17 03:10:03 +00:00
let repeatNum = "42 42 42";
2020-11-27 18:02:05 +00:00
let reRegex = /change/; // Change this line
2019-02-17 03:10:03 +00:00
let result = reRegex.test(repeatNum);
2018-10-04 13:37:37 +00:00
```
2019-07-18 15:24:12 +00:00
2020-11-27 18:02:05 +00:00
# --solutions--
```js
let repeatNum = "42 42 42";
let reRegex = /^(\d+)\s\1\s\1$/;
let result = reRegex.test(repeatNum);
```