DevLab
Regex

Regex Lookahead and Lookbehind: Match Without Consuming

Understand zero-width assertions in regex — how lookahead and lookbehind let you match based on surrounding context without including that context in the result.

What are Zero-Width Assertions?

Most regex patterns consume characters — matching abc advances the cursor three positions. Zero-width assertions match a position in the string without consuming characters. They let you say "match X only when surrounded by Y" without including Y in the result.

Positive Lookahead: (?=...)

Matches a position followed by a specific pattern, without including that pattern in the match.

"16px 2rem 8px".match(/d+(?=px)/g)
// → ["16", "8"]  — "px" is checked but not captured

Negative Lookahead: (?!...)

// Password must have uppercase, lowercase, and a digit
/^(?=.*[a-z])(?=.*[A-Z])(?=.*d).{8,}$/.test("MyPass1word") // → true

// Match "foo" only when NOT followed by "bar"
"foobar foobaz".match(/foo(?!bar)/g) // → ["foo"] (only in "foobaz")

Positive Lookbehind: (?<=...)

"Price: $42.50, $100".match(/(?<=$)d+.?d*/g)
// → ["42.50", "100"]  — "$" is checked but not captured

Negative Lookbehind: (?<!...)

"wildcat housecat".match(/(?

Practical Examples

// Remove leading zeros
"007 042 100".replace(/(?<=^|s)0+(?=d)/g, "")
// → "7 42 100"

// Split on commas not inside parentheses
"a,b,(c,d),e".split(/,(?![^(]*))/)
// → ["a", "b", "(c,d)", "e"]

Browser Support

Lookbehind was added in ES2018: Node.js 10+, Chrome 62+, Firefox 78+, Safari 11.1+. Not in Internet Explorer. Lookahead has been supported since the earliest JavaScript engines.

Practice with these tools

More Learning Topics

RegexRegex Basics: A Complete Beginner's GuideRegexRegex Special Characters: Complete ReferenceRegexRegex Groups and Captures ExplainedRegexRegex Quantifiers: Complete GuideCSSCSS Selectors: The Complete GuideCSSCSS Specificity: Why Your Styles Aren't ApplyingJSONJSONPath Syntax: Query JSON Like XPathTimeUnix Timestamps ExplainedEncodingBase64 Encoding ExplainedEncodingJWT Structure and How It WorksEncodingJWT vs Session Tokens: Which Should You Use?EncodingJWT Refresh Tokens ExplainedCryptoHash Functions Explained: MD5, SHA-256, and When to Use EachEncodingURL Encoding Explained: What %20 Actually MeansJSONJSON Schema Explained: Validate Your JSON DataJSONJSON vs YAML: Which Should You Use?JSONJSON.stringify and JSON.parse: Edge Cases You Should KnowRegexRegex for Email Validation: The Right ApproachCSSThe CSS Box Model: margin, padding, border, and contentCSSFlexbox vs CSS Grid: When to Use EachCSSCSS Custom Properties (Variables) ExplainedTimeISO 8601 Explained: The Right Way to Format DatesTimeUnix Timestamps vs ISO 8601: Which to Use in Your API?EncodingUTF-8 Explained: How Computers Store TextTextCORS Explained: Why Your API Call is BlockedTextHTTP Status Codes: A Practical Developer GuideRegexNamed Capture Groups in Regex: Clean ExtractionsColorsColor Spaces Explained: RGB, HSL, HEX, and BeyondColorsColor Contrast for Developers: WCAG Rules and How to CheckNumbersNumber Bases Explained: Binary, Octal, Decimal, and HexNumbersBitwise Operations for Web DevelopersHTMLHTML Semantic Elements: A Complete ReferenceTextText Encoding for Developers: ASCII, UTF-8, and UnicodeRegexWhat is Regex? Complete Guide for DevelopersJSONJSON Format Explained: Structure, Syntax, and Common ErrorsEncodingHow JWT Works: Header, Payload, Signature DecodedHTMLHow to Convert Any Website to Markdown (for LLMs, RAG & Docs)TextPreparing Website Content for RAG: Clean Markdown Pipelines