Regular Expressions - Reuse Patterns Using Capture Groups

Hello everyone,
I just want to understand the concept behind the output of the match() method. In the example given in the challenge.

let repeatStr = "row row row your boat";
let repeatRegex = /(\w+) \1 \1/;
repeatRegex.test(repeatStr); // Returns true
repeatStr.match(repeatRegex); // Returns ["row row row", "row"]

I do not need the solution for the challenge or any hint I just want to understand the output of the match() method which is [“row row row”, “row”], I get the first element in the array which is “row row row” using the capture group but how the second element (element at index 1 “row”) has been created.
Can someone please explain me how the above syntax work.

Your code so far

let repeatNum = "42 42 42 42";
let reRegex = /(\d+) \1 \1/; // Change this line
let result = reRegex.test(repeatNum);
let res = reRegex.test(repeatNum);
console.log(res);

console.log(repeatNum.match(reRegex));


Your browser information:

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

Challenge: Regular Expressions - Reuse Patterns Using Capture Groups

Link to the challenge:

Thank you so much for the help.

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