pwRegex is causing trouble

Tell us what’s happening:

8pass99 seems to match even when I’ve assigned a first alphabet rule.
Please help me out if I’m making any mistakes with my input.

Your code so far


let sampleWord = "astronaut";
let pwRegex = /(?=[a-z]\w{5,})(?=\D*\d{2})/; // Change this line
let result = pwRegex.test(sampleWord);
console.log("8pass99".match(pwRegex));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36 Edg/81.0.416.62.

Challenge: Positive and Negative Lookahead

Link to the challenge:

the test method will check if a part of the string match the pattern

nothing in your code says that it should be the whole string matching the pattern, you need to specify it

/(?=^\D\w{4,}$)(?=\D*\d{2,})/

This is what I was using earlier for pwRegex, I hope this is what you meant by the entire string being checked to match the pattern. Using this failed a different case, as it was unable to match “astr1on11aut”. Please help me out with this issue.

your second lookahead is checking for “zero or more non digits followed by 2 digits”, which doesn’t consider that digits are allowed before the two consecutive digits. The parameters say “at least two consecutive digits”, not that those two consecutive digits must be the first in the string

/(?=^\D\w{4,}$)(?=\d{2,})/

Should this work then? But it doesn’t as it doesn’t match “bana12” and “abc123” along with the previous failure.

lookaheads start looking at same position, now the second lookahead is checking for two digits at the beginning of the string

1 Like