I need some better explanation

Why exactly does this code work?

let sampleWord = "astronaut";

let pwRegex = /(?=\w{5,})(?=\D*\d{2})/; // Change this line

let result = pwRegex.test(sampleWord);

Specifically, (?=\D*\d{2}). I don’t understand why you would do the \D*\d and not just \d{2}?


/D is Not a Digit it matches anything that is not 0-9. /d is digit, it matches 0-9.

I suggest using a site like https://regexr.com/. You can plugin in your regex and mouseover to see what it does and enter text to test.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.