Confusion with regex lookaheads

I am doing the Regex part of the “Javascript Algorithms and Data Structures Certification” and am stuck on lookahead’s

Does a lookahead not count toward the number of characters matched? When I use the following regex
Regex with lookahead

it matches with 1234 where I would expect it to fail, my logic is that [at least 4 word characters] followed by [2 digits] should mean the string would need to be 6 characters long. It almost feels like the lookaheads are ignoring each other and I do not understand why.

Can someone explain why a 4 digit number is matching?

Which challenge are you specifically trying to pass that is not working?
Make a new post, Share the challenge your trying to pass here it should automatically include the work you did on it so we can better answer your question.

Regarding look aheads there are positive and negative look aheads,
x(?=y) positive lookahead (matches ‘x’ when it’s followed by ‘y’)
x(?!y) negative lookahead (matches ‘x’ when it’s not followed by ‘y’)

there are also look behinds…

Thank you kravmaguy, the particular challenge is Positive and Negative Lookahead however Im stuck on understanding how the lookaheads work rather than not being able to pass the challenge. As I have completed it and moved on, I do not understand why my answer worked.

Does two consecutive lookaheads not work together to make the match? I expected the first part to add with the second part.

Look behinds are not fully supported yet in most modern browsers.

1 Like