Reuse Patterns Using Capture Groups - How do they work?


The challenge itself seems very confusing. I just can’t figure out how they work. I go to the hint and get one sentence explanations. It seems as if it was poorly written.

I tried /(\d+)\3/ because i’m searching for /(\d+) 3 times. That is how i basically understood this problem.

Can someone completely break down what capture groups are and explain them to me?

The parenthesis () and everything inside it is a capture group

You have defined your first capture group, and you need others equal to it, so you call the capture group with \1 (as that is the first capture group, the number you call t with is 1

So for example two consecutive items is /(\d+) \1/ (space included because you want the spaces inside) (four is /(\d+) \1 \1 \1/)

You will have to figure out how to match exactly three things. It is totally doable with things covered in previous lessons, maybe the logic leap to get that may be difficult to do


Thank you so much, i had to use the hints to get it but i now understand it. I have a question.

why does let reRegex = /^(\d+)\s\1\s\1$/; work without the /g but not with it?

In hint three it states:

because we used \g , our Regex doesn’t return after first full match ( test test test ) and matched all repetitions.


I would need to do a bit of Read-Search to answer that, but I imagine it is because g match multiple appearances of your regex

You can see the breakdown here, both with and without the g and see what changes

I just get a error when i do: /^(\d+)\s\1\s\1$/g;

Do you mean in that website? The opening and closing slashes are already included. The flags at the end are to be added with the drop down menu on the side. You can find a guide on how to use it, it will be useful to you if you start using regex often, but it is pretty intuitive

1 Like