Capture group help?

hi everyone
I try this code on the regex101.com and it’s work 100% but in the challenge doesn’t work completely especially with “42 42 42 42” & “101 102 103” & I use capture group twice but all of those pattern work with me on the tool I mentioned above but not on FCC

here is the code:


let repeatNum = "42 42 42";

let reRegex = /(\d+)\s(\d+)\s\1/; // Change this line

let result = reRegex.test(repeatNum);

so where do you think the problem is?

Two things I would suggest about that –

  1. you’re setting up TWO capture groups. The first (\d+) is capturing the first numerical sequence, and the second is capturing a second is capturing a second, which COULD be the same, but doesn’t have to be, followed by a third number, which is a reference to that first capture group. So that would allow 42 42 100 42, and accept the second, third and fourth numbers.

  2. You don’t limit your selection. Within the confines of the challenge, it will be an input that contains exactly three instances of a number. So the limit should be “the input STARTS WITH the number, contains another instance of that same number, and ENDS WITH the same number.” Can you think, with regex, how to limit it to the start and end filter?

1 Like

using that tool, those tests are not passed, try using Unit Tests next time

image

2 Likes

thanks a lot for the explanation :slight_smile:
i gave it a try and construct this /^(\d+)\s(\d+)\s\1$/ which pass all the tests but not this mentioned on this image even if i use two capture groups (\d+)(\d+)

but what is most important for me is I’m not really sure that I understand what I’ve written because I just try and try as the computer do.
so can you explain to me the last part that contains the $ sign what I understand is the final thing should be matched is the next same number matched first because \1 tell to the regex that the upcoming pattern should be the pattern between capture group () am I right? :slight_smile:

thanks a lot @ilenia you help me know this one that saves time to me and make sense more than the simple one :slight_smile:

The \1 says “the first capture group”. Let’s break it down a little:

  1. the // wrapping everything make this a Regex (which is obvious, but we’ll start at the beginning).
  2. the ^ indicates the regex string must START with the following filter, whether a capture group or a value.
  3. the FIRST (\d+)\s says create a capture group consisting of strictly numeric characters, then follow that capture group by any whitespace characters.
  4. the SECOND (\d+)\s says the same thing – so it creates a second capture group consisting of numeric characters. It may or may not be the same as the first capture group, doesn’t actually need to be. If you were doing this intentionally, you now have two separate capture groups, referenced by \1 for the first and \2 for the second.
  5. the \1$ with which the regex ends says “the string should END with the number from the first capture group”. the \1 is the first capture group, and the $ indicates the end of the string.

Take a look at #4 above. That is why you are getting the one test failing.

While it appears all the tests pass, if you were to test with the string 101 202 101, it would also pass – and it shouldn’t.

2 Likes

thanks a lot for your help I really appreciate it, :slight_smile:

finally, before I copy paste this line ^(\d+)\s\1\s\1$ I try to understand it first because I really struggled with this challenge.

so let me try to explain it to you:

^(\d+): it should begin with a set of numbers/digits
\s: after the numbers, there should be a space
\1: means the first number matched should be matched again after the previous space
\s: means followed by space again
\1$: followed by the same first number to be the last one in the string

so we have just 3 items which are equal to each other

I still have a question:
is \1 the one that’s the capture group and not the parentheses ()?

1 Like

The parenthesis and the thing inside are the capture group, the \1 is how you reference the capture group

1 Like

but it passes the test that says “you should reuse capture group twice” even if I use it just once?

The capture group is the part between the parentheses. When you reference \1 twice, that means you are reusing the capture group twice.

To recap: You have a single capture group, but you are reusing (referencing what is captured) twice.

3 Likes

ah yes you’re right because \1 means the first capture group used which is the one between the parentheses

thanks a lot :slight_smile: