Why is *\D used in the solution

Tell us what’s happening:
in the second lookahead \D* or \w* is required. But why?

Even in the explanation \D* is used for the password. why?

i dont get it…
(?=\w{6,})(?=\D*\d{2,})
The first lookahead (?=\w{6,}) searches for numbers, alphabets and space that appears more than 5 times.
The second lookahead (?=\D*\d{2,}) searches for non numbers that appear zero or more times. And searches for numbers that appear twice.
that what i understand.

Your code so far


let sampleWord = "astronaut";
let pwRegex = /(?=\w{6,})(?=\D*\d{2,})/; // Change this line
let result = pwRegex.test(sampleWord);
console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36

Challenge: Positive and Negative Lookahead

Link to the challenge:

\D means “anything that is not a digit”.
\D*\d{2,} means “any number of non-digits (including none) followed by two or more digits”.

You are not alone in asking this question, it is a common source of confusion. I probably can’t explain it as well as this article:

Lookarounds Stand their Ground

Also, I would probably use \w* instead of \D* since I don’t think the requirements rule out that you can have a password composed of all numbers.