Positive and Negative Lookahead question

I’m a little confused at the “Positive and Negative Lookahead” module, namely the example given.

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

I was wondering why the \D* was necessary in the second positive lookahead and ended up playing with it some more, which made me more confused.

let password = "a1bcdf";
let checkPass = /(?=\w{3,5})(?=\d)/;
console.log(checkPass.test(password)); //returns true

but

let password = "abcd1f";
let checkPass = /(?=\w{3,5})(?=\d)/;
console.log(checkPass.test(password)); //returns false

Why did the second code return false? I simply moved the number somewhere else in the string (namely near the end of the string).

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

That explains it perfectly, thank you!

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