Can someone explain this to me?

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

why is \w* there? i wrote the code without it because it asks to use lookaheads in the pwRegex to match passwords that are greater than 5 characters long, and have two consecutive digits.

The \w metacharacter matches word characters.

A word character is a character a-z, A-Z, 0-9, including _ (underscore).

1 Like

yes but it asks for digits not characters? isnt just \d then used?

The \w* is there to allow for any number of word characters (including none) to appear before the two digits. So, this makes sure that there can be any number of word characters between the start of the string and the two digits. If that wasnt there , the regex would only match if the two digits appeared immediately after the first six characters.

2 Likes

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