Parenthesized submatch string(s)?

Hello everyone,

I am doing the regex tutorial, Regular Expressions: Use Capture Groups to Search and Replace. I can’t understand what the following error means: replaceText should use parenthesized submatch string(s) (i.e. the nth parenthesized submatch string, $n, corresponds to the nth capture group). I’ve attached the link to the problem above and my code is below. I appreciate any feedback you may have.

let str = "one two three";

let fixRegex = /^(\D+)\s(\D+)\s(\D+)/; // Change this line

let replaceText = str.replace(fixRegex, '$3 $2 $1'); // Change this line

let result = str.replace(fixRegex, replaceText);

console.log(result);
1 Like

Your replaceText have too much in it. Take a look at replaceText and result, you are kind of trying to do the same twice.

1 Like

why have you twice the replace method?

you should create the result variable by using the regex with capture groups and the dollar signs in the text to change

the replaceText variable should be the string that describe how to replace.
and result the one with the output of the replace method