Regular Expressions: Positive and Negative Lookahead -- incorrect solution

The logic in the solution for this question appears to be incorrect.

It says to “Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long and have two consecutive digits.”

Here’s what I put, which wasn’t accepted.

let sampleWord = "astronaut";
let pwRegex = /(?=\w{6,})(?=\d{2})/; // Change this line
let result = pwRegex.test(sampleWord);

One place I went wrong is {6,} – in the hint it says it’s {5,}. But it says greater than 5 characters (in other words, at least 6), and {5,} means “at least 5 characters.”

I don’t think that’s why your solution failed actually

I think it’s because your second lookahead requires there to be two digits at the current position, and not anywhere further ahead

1 Like

Correct – it did actually pass with

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

However, in my opinion {5,} shouldn’t be accepted.

I see what you mean and I’m inclined to agree

Perhaps it’d be worth attaching that sentiment to this github issue: https://github.com/freeCodeCamp/freeCodeCamp/issues/35700

In that issue we discussed the question as though 5 was the correct value, I guess we overlooked the challenge description

1 Like