Regex Lookahead & Lookbehind Tester
Lookarounds are the part of regular expressions people find hardest to reason about. This tester lets you try positive and negative lookahead and lookbehind against real text and see exactly what matches — including the crucial detail that the assertion checks for context but is never part of the match itself.
How to use the Regex Lookahead & Lookbehind Tester
Type a pattern that uses a lookaround, set the flags you need, and enter some test text. Matches are highlighted in the box below and listed underneath with their position. The four buttons insert the lookaround forms at the cursor so you do not have to remember the exact syntax: (?=…) positive lookahead, (?!…) negative lookahead, (?<=…) positive lookbehind, and (?<!…) negative lookbehind.
The default example, \d+(?=px), matches a run of digits only when followed by "px" — so in 320px it highlights 320 but not the px, and it ignores the 40 in 40em. That is the whole point of a lookaround: it tests the surrounding context but the matched text stops at the assertion, which is why "px" is checked yet never selected.
Everything runs through your browser's own regular-expression engine, so behavior matches JavaScript exactly. Patterns and text never leave your device.
How lookarounds actually work
A lookaround is a zero-width assertion: it tests whether some text could match at the current position without adding that text to the result and without moving the engine's cursor. There are four kinds. Positive lookahead (?=X) succeeds if X comes next; negative lookahead (?!X) succeeds if X does not come next. Positive lookbehind (?<=X) succeeds if X immediately precedes the current spot; negative lookbehind (?<!X) succeeds if it does not. Because they are zero-width, several can stack at the same position, each adding a condition.
The defining feature — and the source of most confusion — is that the asserted text is never consumed. foo(?=bar) matches only the foo in foobar; the bar is required to be there but is left for the rest of the pattern (or the next match) to handle. This is what makes lookarounds so useful for "match X but only when surrounded by Y" problems that a plain pattern cannot express, such as splitting on a boundary you want to keep, or validating that a password contains a digit without specifying where.
Lookbehind has historically been the trickier half. Modern JavaScript (since ES2018), PCRE, .NET, and Python's newer engines support it, and JavaScript even allows variable-length lookbehind, but some engines restrict lookbehind to a fixed length. If a pattern works here but fails elsewhere, a variable-length lookbehind is a likely cause. As a rule, reach for lookarounds when you genuinely need to assert context; for simple matching they add cost and complexity that a straightforward pattern avoids.
Common use cases
- Context-sensitive matching. Grab a number only when it is followed by a unit, or a word only when preceded by a label.
- Password and input rules. Assert that a string contains a digit, an uppercase letter, or a symbol without consuming those characters.
- Splitting while keeping delimiters. Match positions between tokens using lookarounds so the surrounding text is preserved.
- Learning regex. See live which characters a lookaround checks versus which it actually selects.