Somebody to help me to understand what the $ does -- REGEX

Tell us what’s happening:

"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1');
// Returns "Camp Code"

Your code so far

let str = "one two three";
let fixRegex = /change/; // Change this line
let replaceText = ""; // Change this line
let result = str.replace(fixRegex, replaceText);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Use Capture Groups to Search and Replace

Link to the challenge:

This part uses the regex which targets a word and then a space and then another word. Then it applies the second word to the front and the first word to the last. There is A LOT more happening than what I explained, but that’s the basic of what is happening.

\w+ means grab a word one or more times
\s means grab a space
$2 and $1 means to grab the matched words and align . $ means start of a string or end of astring depending on where it is put

1 Like

Thanks :slight_smile: