Freecodecamp: [beta] Regex should reuse the capture group twice

Created on 23 Feb 2017  路  6Comments  路  Source: freeCodeCamp/freeCodeCamp

Challenge reuse-patterns-using-capture-groups has an issue.
User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0.
Please describe how to reproduce this issue, and include links to screenshots if possible.



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

As this regex is completely new to me, I'm not exactly sure if this is a bug, but if it isn't the lessons don't address it.

Here's my error:
Your regex should reuse the capture group twice.

I've gotten all the other tests to pass and I can't figure out how to tell what it's doing, but if all the other tests pass then my assumption is that it is running through the capture group twice.

help wanted

Most helpful comment

I'll take this up..

All 6 comments

@gelbelle Your regex is not complete yet. One of the things you're missing is that you have just one occurrence of \1, that just matches your capture group the second time. You need a total of three matches.

Please visit the Help Room or the HelpJS Room first if you need help with challenges like these, in future. The issue tracker is primarily for reporting bugs with the website itself. Happy Coding!

As Manish mentioned, the capture group here is only reused once, not twice, and they should be separated.
I think the test cases for this challenge could be extended.

  1. A test case the checks whether for missing spaces in between: "424242" should not pass
  2. A test case that confirms the proper length visibly: "42 42" and "42 42 42 42" should not pass

I'll take this up..

Been banging my head on this one for about an hour now. Seemingly solved, but my understanding of the .test method is that "42 42 42 42" contains "42 42 42" and will always return true without further specification. I think I can only test for _at least_ three repetitions in the context of the challenge. All of my attempts to restrict to only three numbers or only two whitespaces causes other tests to fail. I have:

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

Only the "42 42 42 42" test is failing. Is there a solution I'm not seeing?

@bladecla
Your reRegex tests only if it finds 3 numbers in a row. It needs to check whether a string starts with a number and ends with one, while containing 3 numbers. So in 42 42 42 42, it finds a pattern in it even thought its just an substring of it. To fix that instead of let reRegex= /(\d+)\s\1\s\1; use let reRegex=/^(\d+)\s\1\s\1$/; . ^- so that it starts with a number and $ so that it ends with a number.

@voidbringer01
"Your reRegex tests only if it finds 3 numbers in a row. It needs to check whether a string starts with a number and ends with one, while containing 3 numbers. So in 42 42 42 42, it finds a pattern in it even thought its just an substring of it. To fix that instead of let reRegex= /(\d+)\s\1\s\1; use let reRegex=/^(\d+)\s\1\s\1$/; . ^- so that it starts with a number and $ so that it ends with a number."

why is this the case?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MelissaManning picture MelissaManning  路  3Comments

danielonodje picture danielonodje  路  3Comments

SaintPeter picture SaintPeter  路  3Comments

DaphnisM picture DaphnisM  路  3Comments

MichaelLeeHobbs picture MichaelLeeHobbs  路  3Comments