Regular Expressions: Use Capture Groups to Search and Replace.1

Tell us what’s happening:

The code doesn’t work. Please help me.

Your code so far


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);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36.

Challenge: Use Capture Groups to Search and Replace

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/use-capture-groups-to-search-and-replace

Take a closer look at number of brackets in the fixRegex.

1 Like