Positive and Negative Lookahead / Don't understand the positive lookahead used to get 2 consecutive numbers

Hey everyone :wave:,

I know the topic has been quite debated already, but after looking into the forum I don’t get a few things there.

My first attempt was writing:

\


let pwRegex = /^\D(?=\w{6,})(\d{2,})/;

Basically, exclude the first sign as a number, using positive lookahead to have at least 6 signs (so > 5) and then using positive lookahead again to make sure there was at least 2 consecutive number.
This doesn’t pass the test. After checking hints and solution, I change it to


let pwRegex = /^\D(?=\w{6,})(\w*\d{2,})/;

Now it passes, but I don’t get why we need the \w* there as positive lookahead is supposed to make sure the element in the search pattern is there. So just using \d{2,} should already be making sure there’re 2 consecutive number in my search pattern.

Actually I don’t even get why (\w*\d{2,}) is working and why it doesn’t have to (\w*\d{2,}\w*) as nothing says in the instructions that the 2 consecutive digits have to be at the end.

Many many thanks for the help and long life to FCC, such an awesome online resource!

Challenge: Positive and Negative Lookahead

Link to the challenge:

a lookahead doesn’t advance in the string, so the two lookaheads look from the same position, in this case they match from the second character in the string

your first regex constrict the two consecutive numbers to be the 2nd and 3rd character of the string

2 Likes

@ieahleen thanks again for checking my request! I guess I got confused by

Lookaheads are patterns that tell JavaScript to look-ahead in your string to check for patterns further along.

It’s actually tricky to put in words in English, especially for me as French is my native language :sweat_smile: