freeCodeCamp Challenge Guide: Use Capture Groups to Search and Replace

Use Capture Groups to Search and Replace


Problem Explanation

Using .replace() with the first parameter set to find the part of the original string to replace, and the second parameter should be the replacement.


Hints

Hint 1

You will need three capture groups to accomplish this task.

Hint 2

Each capture group will need to be separated by a space character.

Hint 3

The replacement string will use the $ syntax 3 times.


Solutions

Solution 1 (Click to Show/Hide)
let str = "one two three";
let fixRegex = /(\w+)\s(\w+)\s(\w+)/; // Change this line
let replaceText = "$3 $2 $1"; // Change this line
let result = str.replace(fixRegex, replaceText);
10 Likes