Formatters7 min read

Regex Lookahead and Lookbehind: Practical Examples

Master regex lookahead and lookbehind assertions with real-world examples. Test patterns in your browser and learn when to use each type of zero-width assertion.

Try the free online tool mentioned in this guide:Regex Tester

What are zero-width assertions?

Lookahead and lookbehind are zero-width assertions — they match a position in the string rather than consuming characters. A zero-width assertion says "match here only if this pattern appears ahead/behind the cursor," but the matched characters are not included in the overall match.

This makes them powerful for matching words in context: extract prices that follow a dollar sign, usernames that precede an @ symbol, or numbers not followed by a percent sign.

Positive lookahead: (?=...)

A positive lookahead (?=pattern) succeeds only if the pattern appears immediately after the current position. Use it to match something that must be followed by something else without including the follower in the match.

javascript
// Match digits followed by "px"
/\d+(?=px)/

// Input: "margin: 16px solid 8em"
// Matches: "16"  (the "px" is asserted but not captured)

// Match a word before a colon
/\w+(?=:)/

// Input: "name: Alice, age: 30"
// Matches: "name", "age"

Negative lookahead: (?!...)

A negative lookahead (?!pattern) succeeds only if the pattern does NOT appear immediately after the current position. Use it to exclude unwanted contexts.

javascript
// Match "file" not followed by ".exe"
/file(?!\.exe)/i

// Input: "file.txt file.exe file.log"
// Matches: "file" in "file.txt" and "file.log" only

// Match numbers not followed by "%"
/\d+(?!%)/

// Input: "score: 95% items: 42"
// Matches: "42" (95 is followed by %, so skipped)

Positive lookbehind: (?<=...)

A positive lookbehind (?<=pattern) matches only if the pattern appears immediately before the current position. Use it to match text that must be preceded by something specific.

javascript
// Match digits preceded by "$"
/(?<=\$)\d+(\.\d{2})?/

// Input: "prices: $19.99 and $5"
// Matches: "19.99", "5"

// Match word after "Dr. "
/(?<=Dr\.\s)\w+/

// Input: "Patients: Dr. Smith and Dr. Jones"
// Matches: "Smith", "Jones"

Negative lookbehind: (?<!...)

A negative lookbehind (? matches only if the pattern does NOT appear before the current position.

javascript
// Match "port" not preceded by "air"
/(?<!air)port/

// Input: "airport seaport"
// Matches: "port" in "seaport" only

// Match numbers not preceded by "-"
/(?<!-)\d+/

// Input: "balance: -42 count: 100"
// Matches: "100"

Browser support and JavaScript notes

Lookbehind ((?<=...) and (?) requires ES2018+ (Chrome 62+, Firefox 78+, Node 8+). Positive and negative lookahead have been supported in JavaScript since ES3 and work in all browsers.

For complex patterns, test them in an online regex tester before embedding them in code. MyDevTools Regex Tester lets you paste input, write the pattern, and see matches highlighted in real time with match groups, indices, and explanations.

Practical use cases

  • Password validation: require at least one digit (?=.*\d), one uppercase (?=.*[A-Z]), one special character.
  • Log parsing: extract error codes that appear after a specific prefix without capturing the prefix.
  • URL slug generation: strip characters not followed by alphanumerics.
  • Currency extraction: grab numeric amounts that follow $, , or £ without including the symbol in the capture.
  • Negative filter: find lines containing a word but not preceded by "not" or "no ".

Frequently asked questions

Do lookbehind assertions work in JavaScript?

Yes, since ES2018. Positive lookbehind (?<=) and negative lookbehind (?<!) work in Node 8+ and all major browsers released after 2018. Lookahead has been supported since ES3.

What is the difference between lookahead and capturing groups?

A capturing group (parentheses without ?) includes its match in the result and consumes characters. A lookahead is zero-width — it checks for a pattern at that position but does not consume characters or include them in the match result.

Can I chain multiple lookaheads in one regex?

Yes. Multiple lookaheads can be applied at the same position: /(?=.*\d)(?=.*[A-Z])(?=.*[^a-zA-Z0-9])/ is a common pattern for password validation that checks for a digit, an uppercase letter, and a special character.

Try Regex Tester for free

Test JavaScript regular expressions with live match highlighting, flags (g, i, m, s, u), and match counts. Client-side only. No install, no account required to try it.