Tell us what’s happening:
I don’t understand what the dollar sign does? Can someone give me another example because the example that is given doesn’t make any sense to me.
Why are we using a capture group when they are used to look for repeat substrings?
Your code so far
let str = "one two three";
let fixRegex = /(\w+)\s(\w+)\s(\w+)/; // 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
let first_last = "Bob Smith";
let r = /([a-z]+)\s([a-z]+)/i;
let last_first = first_last.replace(r, "$2, $1");
console.log(last_first); // Smith, Bob
the dollar sign plus number symbols are placeholders, and refer back to the capture groups, so $1 will refer to the first capture group, and $2 to the second capture group, they can be used to take parts of the original string and still keep them but maybe add something, or move them around
If you use \w you are typing less, and it includes all word characters (it’s equal to [a-zA-Z0-9_])
it depends on what you need to do. In the case of this challenge both \w and [a-z] give the same result so it doesn’t matter.
Can you help me clarify then, the difference between the “.” and +
I thought that the + was looking for multiple of the same thing and the . was just including whatever comes after the matched element.