Regex - look aheads

I think the answer for the “Regular Expressions: Positive and Negative Lookahead” is wrong (even though it passes all the tests). The is because the question states that it cannot begin with a number. I think for this reason the * sign should be a + sign, and perhaps another test should be added e.g. “123456”

Blockquote
let sampleWord = “astronaut”;
let pwRegex = /(?=\w{6,})(?=\D*\d{2})/;
let result = pwRegex.test(sampleWord);

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/positive-and-negative-lookahead

Well it’s JS there are alway’s several way’s to write the right code. Doesn’t mean that you are wrong doesn’t mean that the code is wrong.

I’ve updated the solution.

1 Like

@ArielLeslie What missing test case would need to be added to force addition of the ^ at the beginning of the regex (as you changed in the solution)? I guess what I mean is, what case would the original solution incorrectly pass/fail? I ask, because if we need to update the tests, I would like to add the test case.

If you had a password that was more than 6 characters long, and had 6 consecutive characters that met the requirements. Example: “8pass99”

1 Like

@ArielLeslie Great catch! I have created a GitHub issue so someone can add the missing test case and correct the solution in the challenge file.

@ArielLeslie Whenever we make changes to solutions, we should review the challenge file and open issues to discuss what might need to be changed in the tests and/or solutions for the challenge. We should probably update the moderator guideline to reflect this.

Good call.

Thanks for looking into this guys, and good catch with the ^ symbol. However, I still don’t think this catches the test - which doesn’t exist, but should - of: ‘123456’).
I just ran it and still comes through as ‘true’. Even though it starts with a number (question states that it can’t start with a number).

Perhaps there should be a new test for “123456” that the regex should not match? I’d be interested to hear your thoughts:

Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long, do not begin with numbers, and have two consecutive digits.

The * should be changed to a +.

Here is a regex 101 test set: https://regex101.com/r/kQF6kF/1

Any other problems anyone can think of?

It will probably help to explicitly state in the error message that ONLY two positive lookaheads can be used (and nothing outside of that).
As far as I can tell this solution works in all cases, except in the FCC test env:
/(?=\w{5,})^\D+\w*(?=\d{2,})\w*/i