Regex to check whether username obeys the criteria

Question: Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long and have two consecutive digits.

Answer:

let sampleWord = "astronaut";
let pwRegex =  /(?=\w{5,})(?=\D*\d{2})/;      
let result = pwRegex.test(sampleWord);

Can anyone please explain why \D* is used in the second look-ahead ? What does it do exactly ?

\D is the shorthand for non-digit characters, and * means “0 or more”

1 Like

Why use non-digit \D when we actually need two digits in the end ?

The two digits are matched with \d{2}
Together, two digits preceded by non digits

1 Like