Regular expressions have a reputation for being unreadable, and much of it is earned. A single pattern can cover half a screen in backslashes and brackets, and when it breaks the failure is usually silent: no match, no error, no clue. Lookahead and lookbehind are assertions that change that. They do not consume characters; they look around the current position and answer yes or no, so you can write conditions such as “match this word only when a colon follows it” or “match this number only when no currency sign precedes it.”
This guide explains how the two assertions work, walks through concrete patterns, and covers the support gaps you will meet in real browsers and tools. Because lookarounds describe context without becoming part of the match, they keep substitutions clean. Every example can be tested in our regex tester without signing up.
What Lookahead and Lookbehind Actually Are
Lookahead and lookbehind are zero-width assertions. That is the most important sentence in this article. A normal token such as \d or [a-z] consumes a character: the engine moves forward, and the matched text becomes part of the result. An assertion consumes nothing. It checks a condition at the current position and leaves the engine exactly where it started.
Picture proofreading with your finger on the page: you look ahead to see the next word, but your finger does not move. If the condition holds, the match may proceed; otherwise the engine backtracks and tries elsewhere. Because nothing is consumed, you can stack conditions on the same spot — for example, requiring that a password contain a digit somewhere without dictating where it appears. The match result itself starts and ends at the same token regardless of which assertion you use.
Lookahead: Checking What Comes Next
There are two forms. Positive lookahead (?=...) succeeds when the pattern inside matches ahead at the current position; negative lookahead (?!...) succeeds when it does not.
To find labels in a config file, match a word only when a colon follows: \w+(?=\s*:). Against “timeout: 30” the match is “timeout”, not the colon. To find three-digit codes that are not discount percentages, use \b\d{3}(?!%)\b — “250” matches, “250%” does not. Or find function names followed by an opening parenthesis: \w+(?=\().
Because the lookahead text never becomes part of the match, you can check context without capturing it. That is handy when you need the surrounding condition but only want the word itself. One more example: match a username only when an @ sign follows, with \w+(?=@).
Lookbehind: Checking What Came Before
Lookbehind mirrors lookahead in the opposite direction. Positive lookbehind (?<=...) succeeds when the text immediately before the current position matches; negative lookbehind (?<!... succeeds when it does not.
To grab an amount after a dollar sign without keeping the sign: (?<=\$)\d+(\.\d+)? — against “$12.50” the match is “12.50”. To match a word only when “no ” does not come before it: (?<!no\s)\w+.
Lookbehind has weaker historical support than lookahead, and some engines still require a fixed width inside the assertion — no quantifiers such as * or +. Variable-length lookbehinds work in some engines and not others, so when in doubt, test the exact pattern in your target environment. Keep the lookbehind pattern fixed-length where you can, and check the engine you are targeting.
Patterns You Can Use Today
1. Password strength — at least eight characters, one digit, one uppercase letter: ^(?=.*\d)(?=.*[A-Z]).{8,}$. Two lookaheads scan the same string from the same start position without consuming anything.
2. Words inside quotation marks, without the quotes: (?<=")\w+(?="). The lookbehind verifies an opening quote and the lookahead verifies a closing one.
3. Currency amounts not already formatted: match \d+\.\d{2} only when a dollar sign does not precede it: (?<!\$)\d+\.\d{2}.
Treat these as starting points. Real input is messier than examples, so test with your actual data.
Support, Pitfalls, and When to Skip Them
Lookahead is widely supported; lookbehind support is good in modern browsers but younger, and older environments may not recognize it at all. Variable-length lookbehinds work in some engines and not others, so fixed-width lookbehinds are the safer choice when you need broad compatibility.
Lookarounds also carry a readability cost. A pattern dense with (?= and (?<! is hard to review six months later, and on very large inputs each assertion can trigger backtracking at every position. Flags such as case-insensitive or multiline apply to the whole pattern, including the lookaround portions, so test with the flags you intend to use. Typically, if a plain capture group plus a little post-processing does the job, prefer that and keep the regex easy to read.
Test Before You Deploy
Regex bugs surface in production data, not in your editor. Test with matching and non-matching samples, edge cases such as empty strings and repeated matches, and check your engine flags — global, case-insensitive, multiline. The habit is the same one our Cron Explainer encourages: verify behavior before you deploy, not after. Our regex tester highlights matches as you type, so a bad pattern is obvious immediately instead of showing up in logs.
Paste your pattern. See the matches. No signup.
Test Regex FreeRegex Lookaround FAQs
What is the difference between lookahead and lookbehind?
Lookahead checks what follows the current position; lookbehind checks what precedes it. Neither consumes characters, so neither changes where the match starts or ends.
Does JavaScript support lookbehind?
Yes. Lookbehind assertions arrived in ECMAScript 2018, so all modern browsers support fixed-width lookbehind. For very old environments, restructure the pattern with capture groups instead.
Can lookarounds be nested?
Typically yes — an assertion can sit inside another assertion, and lookahead and lookbehind can combine at the same position. The result becomes hard to read quickly, so comment the pattern or split it into steps.