2018-10-10 22:03:03 +00:00
---
id: 587d7dbb367417b2b2512bab
2021-02-06 04:42:36 +00:00
title: Use Capture Groups to Search and Replace
2018-10-10 22:03:03 +00:00
challengeType: 1
2020-08-04 07:14:01 +00:00
forumTopicId: 301368
2021-01-13 02:31:00 +00:00
dashedName: use-capture-groups-to-search-and-replace
2018-10-10 22:03:03 +00:00
---
2020-12-16 07:37:30 +00:00
# --description--
2021-02-06 04:42:36 +00:00
Searching is useful. However, you can make searching even more powerful when it also changes (or replaces) the text you match.
2020-12-16 07:37:30 +00:00
2021-02-06 04:42:36 +00:00
You can search and replace text in a string using `.replace()` on a string. The inputs for `.replace()` is first the regex pattern you want to search for. The second parameter is the string to replace the match or a function to do something.
2020-08-04 07:14:01 +00:00
```js
let wrongText = "The sky is silver.";
let silverRegex = /silver/;
wrongText.replace(silverRegex, "blue");
// Returns "The sky is blue."
```
2021-02-06 04:42:36 +00:00
You can also access capture groups in the replacement string with dollar signs (`$`).
2020-08-04 07:14:01 +00:00
```js
"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1');
// Returns "Camp Code"
```
2020-12-16 07:37:30 +00:00
# --instructions--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
Write a regex `fixRegex` using three capture groups that will search for each word in the string "one two three". Then update the `replaceText` variable to replace "one two three" with the string "three two one" and assign the result to the `result` variable. Make sure you are utilizing capture groups in the replacement string using the dollar sign (`$`) syntax.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
# --hints--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
You should use `.replace()` to search and replace.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert(code.match(/\.replace\(.*\)/));
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
Your regex should change `"one two three"` to `"three two one"`
2018-10-10 22:03:03 +00:00
```js
2021-02-06 04:42:36 +00:00
assert(result === 'three two one');
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
You should not change the last line.
2018-10-10 22:03:03 +00:00
```js
2021-02-06 04:42:36 +00:00
assert(code.match(/result\s*=\s*str\.replace\(.*?\)/));
```
`fixRegex` should use at least three capture groups.
```js
assert(new RegExp(fixRegex.source + '|').exec('').length - 1 >= 3);
```
`replaceText` should use parenthesized submatch string(s) (i.e. the nth parenthesized submatch string, $n, corresponds to the nth capture group).
```js
{
const re = /(\$\d{1,2})+(?:[\D]|\b)/g;
assert(replaceText.match(re).length >= 3);
}
2018-10-10 22:03:03 +00:00
```
2020-08-04 07:14:01 +00:00
2021-01-13 02:31:00 +00:00
# --seed--
## --seed-contents--
```js
let str = "one two three";
let fixRegex = /change/; // Change this line
let replaceText = ""; // Change this line
let result = str.replace(fixRegex, replaceText);
```
2020-12-16 07:37:30 +00:00
# --solutions--
2021-01-13 02:31:00 +00:00
```js
let str = "one two three";
let fixRegex = /(\w+) (\w+) (\w+)/g; // Change this line
let replaceText = "$3 $2 $1"; // Change this line
let result = str.replace(fixRegex, replaceText);
```