Why *w is required for this regex to work

I’m currently on the positive and negative lookahead lesson in the javascript curriculum, and the command is: Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long, and have two consecutive digits.

Originally, my code was:

let sampleWord = “astronaut”;

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

let result = pwRegex.test(sampleWord);

but that doesn’t work, and the given solution is:

let sampleWord = “astronaut”;

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

let result = pwRegex.test(sampleWord);

Why does this work? My second part looks ahead to make sure there are at least two digits right? What difference does the \w* make? I’m having trouble understanding

The two look ahead start looking from same position, so in the first version the two digits must be at the beginning

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