Look at this regexp:
console.log(/^a(?=b)$/.test('ab'));
I have tried it on an online tool. This tool says my 'ab' string matches. But it displays false in javascript.
Any idea ?
Thanks
Look at this regexp:
console.log(/^a(?=b)$/.test('ab'));
I have tried it on an online tool. This tool says my 'ab' string matches. But it displays false in javascript.
Any idea ?
Thanks
A lookahead does not consume anything, so the $
won't match because there's still the b
left.
You can see it if you omit the $
:
console.log(/^a(?=b)/.test('ab'));
In more detail you ask your engine the following questions:
^
)? - Yes it does.a
coming? - Yes there is.a
a b
lurking around the corner? - Yes there is.b
is still round the corner, my poor boy!So the engine cannot report a match and is left crying on the floor because her behaviour was called strange in the first place where she made everything correct.