Why aren't my lookaheads working?

Hello,

my regex /(?=^\D)(?=\w{5,})(?=\D*\d\d)/ isn’t testing true on the word “astr1on11aut”. Why is it? When you take the lookaheads individually they do result true, but when you put them together they result false:


/(?=^\D)(?=\w{5,})/.test("astr1on11aut"); // = true (lookaheads 1 & 2)
/(?=\D*\d\d)/.test("astr1on11aut"); // = true (la 3)
 /(?=^\D)(?=\w{5,})(?=\D*\d\d)/.test("astr1on11aut"); //= false (la 1, 2 & 3)

Your code so far


let sampleWord = "astr1on11aut";
let pwRegex = /(?=^\D)(?=\w{5,})(?=\D*\d\d)/; // Change this line
let result = pwRegex.test(sampleWord);

Challenge: Positive and Negative Lookahead

Link to the challenge:

all lookaheads match the same position, as you have one lookahead with the anchor that match beginning of string, it means that they all must match a pattern starting at the beginning of the string

you have a first one that say ^\D, first character is not a number.

then you have one that say \w{5,}, so at least five characters

than you have the last one, that say \D*\d\d
this means “zero or more non number characters followed by two number characters” - it means that you are imposing that the first numbers in the string must be the two consecutive numbers

you need to relax this one. the two consecutive numbers can be anywhere in the string and there can be other numbers before and after.

this is the reason why that string fails, because you have one number before the two consecutive numbers.