[Help Wanted]Reuse Patterns Using Capture Groups

Tell us what’s happening:
Sometimes freecodecamp irritates more than educate. I am frustrated with this; unable to pass this challenge. Please suggest the solution. Also, explain your code with examples.

Your code so far


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

error

// running test
Your regex should not match “42 42 42 42”.
// tests completed

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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

i totally get it. I struggled with this too.

The trick is to understand that they want only three repetitions.
So currently your code fails if 4 repetitions of 42 are given , correct?

Think about how to make the pattern work if given 3 only . (how do you make it fail otherwise?)

I can give further hints if needed…

I haven’t even understand the ‘capture groups’ yet. The code you see is the accidental, I don’t even know how is this code working for/passing all the conditions/checklist, except the one (error mentioned).

ok so you just need to read a bit more about the group capture method. See here:
there are many online articles about this, but try this one and see if it helps you a bit
https://javascript.info/regexp-groups

Remember, you can always google…

Meanwhile I am reading this, I have to say that, “some challenges on freecodecamp is supereasy and some are superdifficult(according to the tutorial/info/example provided). It should be more clear. Also the output has been stopped which creates more confusion.”

i have to agree. but the only thing we can do as learners, is learn as much as we can and then contribute to the freecodecamp code when we are able to

1 Like

Good article, but I am Still unable to figure it out.

The “group” being referred to is enclosed in brackets ()

\1 then refers to whatever was found in the brackets

If you have more than one group, you’d use \2 to refer to the second group, and so on

I just wanted a solution. Because, I have tried and tested many regex at https://regex101.com/ .But applying in the challenge none of them are fulfilling all the conditions.

According to what have I learned from ‘capture groups’, this solution should work

let repeatNum = “42 42 42”;
let reRegex = /^(\d+)(\s)\1\2\1$/;
let result = reRegex.test(repeatNum);

Yes it should work. To help you out though, the person who wrote this challenge was only expecting you to use one capture group. So hardcode the repeated spaces…

Why was I summoned in particular? Seems like you’ve got a working answer, and I’m glad, but I don’t need to be assigned homework.