Difference between (\w+) and \w+

Tell us what’s happening:
Describe your issue in detail here.
Dear all,

I have question regarding the following code, I’m wondering what is the difference between (\w+) and without parentheses? I can’t find the info about this.
Thanks for replying.

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/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15

Challenge: Use Capture Groups to Search and Replace

Link to the challenge:

with the parenthesis it is a capture group, and so it can be referenced with the numbers. add a console.log(result) at the end and try to remove the parenthesis and see what happens

Catastrophic backtracking (javascript.info)

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