Positive and Negative Lookahead

I’ve read some of the forums on this topic already but I still do not seem to understand how this works completely.

So my attempt for this solution was: /(?=\w{5,})(?=\d{2})/ and if you skip the part where it says {5,} the code should run fine as the requirements are:

passwords that are greater than 5 characters long, and have two consecutive digits

However, the password check seems to not match the at ‘bana12’, amongst the other ones naimly ‘abc123’, and ‘8pass99’.
If I understood correctly, the \w notation should check for all the characters, not only word characters but also digit characters (0-9). Next to that the lookahead \d{2} checks for exactly two consecutive digits.

Now the provided solution is:


Which to me is understandable to be correct IF we say that the two lookahead conditions work as an OR statement rather then AND. How I understand this is that, the first part checks whether there are 6 consecutive characters, meanwhile the second one makes sure that there are some or no characters before the digits (which still seems a bit redundant considering that \w accounts for digits as well). One of the forums said that this is because the lookaheads begin from the same point and if that point is 0(the very first character of every password), then how do they function as AND statements rather than OR statements? Cause what the statement above is saying is, make sure that the password has 6 characters (regardless if they are word or digit cause \w accounts for all) starting from the first one, AND make sure that the digits follow up after 0 or more characters.
But if you think about it, my regex expression says the same thing:
make sure that the password has 5 or more characters (regardless if they are word or digit cause \w accounts for all) starting from the first one, AND make sure that there are two digits, regardless of whether there are characters preceding it or not. So I would understand the obvious mistake of length of the password, that I mentioned earlier, but putting that aside, why wouldn’t the password bana12 not go through for example? Password bana12 has 6 characters in total (again, \w should account for both word and number characters, according to the fCC lesson “Match All Letters and Numbers”), AND bana12 consists of specifically two digits 1 and 2.

I know this might seem obvious to some, but Im still struggling with this, so any help is appreciated.

Wrong part:

/(?=\w{5,})/    //  >= 5 char long  . WRONG.
/(?=\d{2}) /    // it checks if FIRST 2 characters are digit. But 2 consecutive digit could be at location of middle or at end.

Correct part:

/(?=\w{6,})/    //  >  5 char long. 
/(?= )(?= )/    // this is AND for 2 lookaheads. Because lookahead don't actually match any characters, so you still at beginning of your string.

First, it is cool to learn to use https://regex101.com/ to test your regex.

About basic lookahead you can watch the end part of this, Learn Regular Expressions In 20 Minutes - YouTube

I learn advanced lookaheads from here Mastering Lookahead and Lookbehind

Here’s a link to my explanation in another thread. Hope it helps.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.