Files
.github
api-server
client
config
curriculum
__fixtures__
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
basic-javascript-rpg-game
debugging
es6
functional-programming
functional-programming-spreadsheet
intermediate-algorithm-scripting
intermediate-javascript-calorie-counter
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-quality-assurance
07-scientific-computing-with-python
08-data-analysis-with-python
09-information-security
10-coding-interview-prep
11-machine-learning-with-python
12-certificates
portuguese
russian
spanish
schema
test
.babelrc
LICENSE.md
comment-dictionary.js
create-challenge-bundle.js
getChallenges.acceptance.test.js
getChallenges.js
getChallenges.test.js
gulpfile.js
lib.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
crowdin.yml
cypress-install.js
cypress.json
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
jest.config.js
lerna.json
lighthouserc.js
package-lock.json
package.json
sample.env
freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/regular-expressions/use-capture-groups-to-search-and-replace.english.md

86 lines
2.7 KiB
Markdown
Raw Normal View History

---
id: 587d7dbb367417b2b2512bab
title: Use Capture Groups to Search and Replace
challengeType: 1
forumTopicId: 301368
---
## Description
<section id='description'>
Searching is useful. However, you can make searching even more powerful when it also changes (or replaces) the text you match.
You can search and replace text in a string using <code>.replace()</code> on a string. The inputs for <code>.replace()</code> 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.
```js
let wrongText = "The sky is silver.";
let silverRegex = /silver/;
wrongText.replace(silverRegex, "blue");
// Returns "The sky is blue."
```
You can also access capture groups in the replacement string with dollar signs (<code>$</code>).
```js
"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1');
// Returns "Camp Code"
```
</section>
## Instructions
<section id='instructions'>
Write a regex <code>fixRegex</code> using three capture groups that will search for each word in the string "one two three". Then update the <code>replaceText</code> variable to replace "one two three" with the string "three two one" and assign the result to the <code>result</code> variable. Make sure you are utilizing capture groups in the replacement string using the dollar sign (<code>$</code>) syntax.
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: You should use <code>.replace()</code> to search and replace.
testString: assert(code.match(/\.replace\(.*\)/));
- text: Your regex should change <code>"one two three"</code> to <code>"three two one"</code>
testString: assert(result === "three two one");
- text: You should not change the last line.
testString: assert(code.match(/result\s*=\s*str\.replace\(.*?\)/));
- text: <code>fixRegex</code> should use at least three capture groups.
testString: assert((new RegExp(fixRegex.source + '|')).exec('').length - 1 >= 3);
- text: <code>replaceText</code> should use parenthesized submatch string(s) (i.e. the nth parenthesized submatch string, $n, corresponds to the nth capture group).
testString: '{
const re = /(\$\d{1,2})+(?:[\D]|\b)/g;
assert(replaceText.match(re).length >= 3);
}'
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
let str = "one two three";
let fixRegex = /change/; // Change this line
let replaceText = ""; // Change this line
let result = str.replace(fixRegex, replaceText);
```
</div>
</section>
## Solution
<section id='solution'>
```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);
```
</section>