Regular Expressions - Use Capture Groups to Search and Replace

Hi, for a strange reason, this code is not working. I tried switching to strings and even the order.
The output says:

" // running tests Your regex should change the string

one two three

to the string

three two one

// tests completed

Any ideas what could be wrong? I tried looking for other solutions but still trapped in it. Can’t skip this exercise though

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_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Regular Expressions - Use Capture Groups to Search and Replace

Link to the challenge:

I don’t know if it’s logic mistake or typo, but this:

is not OK

When you say “not OK” what do you mean?

This part of code is incorrect I think, it should be changed.
Do you think I am incorrect code-wise? Or maybe this was poor choice of words from me?

Andrey you were right. My mistake was using “+” outside the “( )”. That little detail I foolish missed. Can’t believe I haven’t realized it, but I guess this happens a lot.
Thank you!!

*In case somebody needs to know:

new code:
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);

Good to know you figured it out! But try not to post working solutions if you don’t have additional questions about it. It can be unwanted spoiler for other people. Thanks.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.