Positive and Negative Lookahead theory not clear

Positive and Negative Lookahead theory not clear


Tell us what’s happening:

in the theoy part

A more practical use of lookaheads is to check two or more patterns in one string. 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); // Returns true

i don’t understand why (?=\D*\d) is used?..because (?=\D*) returns true if there is zero or more non digits.
But we have already checked non digits using (?=\w{3,6}).
if there would have been (?=\d+) then it would have matched one or more digits…which is the requirement by the password checker.

Link to the challenge:


\w includes letters, digits, and the underscore character.


what about this (?=\D*\d)


This has been confusing me too. The \D* seems redundant but if I don’t use it my tests don’t pass but I can’t figure out why specifically bana12 and abc123 require the optional non-digit regex to pass.