Positive and Negative Lookahead

This is really good you are working with regex too, very good.

The case study says:

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

It means the word should be at-least 6 character, first note a character could be either digit or letter.

It also says it should have two digits together, and as it does not stated where?(start or end), you may assume anywhere.

First let’s start with (more than)5 character long(at-least 6, thanks @JPili for pointing out) , easy this could be .{5,}.{6,} as . means anything, and the count.

Now the two digits which is the easy part and you have already did, it should \d{2,} well done.
The one you have matches the digit at the begin. for having the digits in any place, we should tell two digits in any place.

I’m not regex expert, but I tried .*(anything, any count) before and and after the two digit pattern.

So the final pattern could be something like following
/(?=.{5,})(?=(.*\d{2,}.*))/
/(?=.{6,})(?=(.*\d{2,}.*))/
or
/(?=\w{5,})(?=(.*\d{2,}.*))/
/(?=\w{6,})(?=(.*\d{2,}.*))/
with means anything longer than 5 character, that includes two digit at any place (begin, center, end)

Keep going on great work, happy programming.

EDIT:
Thanks a million @JPili for pointing out the issue, the challenge needs more than 5 length, I got it as equal or more than 5, sorry, thanks.

22 Likes