Capture group issu

Capture group issu
0

#1

Hi fellow Campers,
I am stuck at Capture groups challenge
Here is my solution:

let repeatNum = "101 102 103";
let reRegex = /(\d+)\s(\d+)\s\1/; // Change this line
let result = reRegex.test(repeatNum);
let result1 = repeatNum.match(reRegex);

console.log(result,result1);

And I admit that I don’t fully understand what’s happening and didn’t find a good tut to simplify it to me. So if you know one please attach the link.
Any help appreciated


#2

Can you paste a link to the exercise?


#3

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/reuse-patterns-using-capture-groups


#4

Capture groups are created when you use just parenthesis. The idea behind it is to be able to reuse that group throughout the regex much like creating a variable.

let regex = /(yaba)(daba)\1\2\1/

In this regex the “\1” refers to the first set of parenthesis, and the “\2” refers to the second. this is equivalent to making a regex that has this.

let regex = /yabadabayabadabayaba/

I use RegExr to help me build regex when I need to. It allows you to see what will match without running console logs over and over.


#5

Bro I am not sure that I understand it well but the website is awesome and very helpful. Thank you