FreeCodeCamp Regex Help

Positive and Negative Lookahead

Can someone explain how the below regex expression enforces a “password” to more than 5 characters with 2 consecutive digits? It also restricts the password from starting with digits, but I understand the ^\D part. The lookahead confuses me because \w{5} makes it seem like the password has to be exactly 5 characters (not more than). I’m also confused by the latter lookahead \w*\d{2} which reads “any number of characters followed by exactly 2 digits”. Is the ‘\w*’ necessary since the beginning already enforces that it can’t start with a digit?


A really handy tool for working with regular expressions is regex101. It breaks down all of the rules of the pattern. Here is yours:

The \w* is what allows for passwords that have more than 5 non-word characters. There must be 5, but after that there can be as many more as you want.