RegEx Question - Mine slightly different than solution. Why doesn't mine work

Tell us what’s happening:
My regex didn’t pass all of the tests. I’d like to tell you what I did in plain English and compare it to the solution, but please tell me why mine doesn’t work.

Challenge: Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long, do not begin with numbers, and have two consecutive digits.

My Regex
/^a-z(?=\d{2,})/gi
Must start with a letter
Lookahead to see if there are five or more alphanumeric characters
Lookahead to see if there are 2 consecutive digits.

Solution Regex
/^\D(?=\w{5})(?=\w*\d{2})/
Cannot start with a digit similar result as ^[a-z] in mine
Lookahead for five alphanumeric characters I look for 5 or more.
Lookahead for zero or more alphanumeric characters Haven’t I done similarly by looking for 5 or more alphanumeric characters?
Followed by 2 consecutive digits I look ahead for two consecutive digits

How are they functionally different enough that mine doesn’t pass all the tests.

Your code so far


let sampleWord = "astronaut";
let pwRegex = /^[a-z](?=\w{5,})(?=\d{2,})/gi; // Change this line
let result = pwRegex.test(sampleWord);


bana12
/^[a-z](?=\w{5,})(?=\d{2,})/
/^\D(?=\w{5})(?=\w*\d{2})/

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15.

Challenge: Positive and Negative Lookahead

Link to the challenge:

the lookaheads are zero width elements, they start checking from same position
as it is your second lookahead will match numbers at index 1 and 2, instead they can be anywhere in the string

also, do not use g flag with test method

Thank you, magical girl. I still had trouble with understanding “zero width” and how my 2nd lookahead would look back to index 1 and 2. So if anyone else is following this thread, this description from rexegg.com helped me make sense of it.

Lookarounds often cause confusion to the regex apprentice. I believe this confusion promptly disappears if one simple point is firmly grasped. It is that at the end of a lookahead or a lookbehind, the regex engine hasn’t moved on the string. You can chain three more lookaheads after the first, and the regex engine still won’t move. In fact, that’s a useful technique.