Regular Expressions - Positive and Negative Lookahead

Hi! I’m currently going through the JavaScript course and although I don’t need any help in solving the exercises, there’s just something I don’t understand here:

"Here is a (naively) simple password checker that looks for between 3 and 6 characters and at least one number:

let password = "abc123";
let checkPass = /(?=\w{3,6})(?=\D*\d)/;
checkPass.test(password);

"

To my understanding, if we wanted to look for at least one number in a password, we would use this syntax:

(?=\d+)

Or, alternatively:

(?=\d{1,})

So I guess my question is: why the “\D*”? What’s the purpose of it and why not use what I mentioned above instead?

I’d really appreciate your help with this!

Too long to explain again. Recycling answer from another post .

Note, my example uses \w* instead of \D* but the principle is the same.

That was an amazing explanation. It never occurred to me that lookaheads could have different “starting” points, so it all makes sense now.

Thank you so much for that! :smiley:

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