Check what follows without taking it
x(?=y) matches x only if y comes next, but y is not part of the match. (?!y) is the negative.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
The number before a unit, a word not followed by a suffix, password rules like 'must contain a digit'.
How to think about it
Is the context part of the match, or only a condition? When you want a condition on the context but a clean match, use lookahead. When you need the context in the match, do not.
Worked example
pattern: \d+(?= ms)Digits, but only if ' ms' follows.
text: took 250 msMatches 250 only.
pattern: ^(?=.*\d).{8,}$At least 8 characters and somewhere a digit: the password rule shape.Your turn
A word followed by a colon, without the colon.
\w+(:)
Test a pattern against real text
The trap
Lookbehind (?<=...) exists too, but not every engine supports it. Lookahead is universal.
Practise lookahead on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.