HeoLab
ToolsBlogAboutContact
HeoLab

Free developer tools with AI enhancement. Built for developers who ship.

Tools

  • JSON Formatter
  • JWT Decoder
  • Base64 Encoder
  • Timestamp Converter
  • Regex Tester
  • All Tools →

Resources

  • Blog
  • What is JSON?
  • JWT Deep Dive
  • Base64 Explained

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Service

© 2026 HeoLab. All rights reserved.

Tools work in your browser. Zero data retention.

HomeBlogMastering Regex Lookahead and Lookbehind
Table of Contents▾
  • Table of Contents
  • What are Lookarounds?
  • Positive Lookahead
  • Negative Lookahead
  • Positive Lookbehind
  • Negative Lookbehind
  • Real-World Examples
deep-dives#regex#lookahead#lookbehind

Mastering Regex Lookahead and Lookbehind

A practical guide to lookahead and lookbehind assertions — the most powerful regex features with real examples.

Trong Ngo
February 22, 2026
2 min read

Table of Contents

  • What are Lookarounds?
  • Positive Lookahead
  • Negative Lookahead
  • Positive Lookbehind
  • Negative Lookbehind
  • Real-World Examples

What are Lookarounds?

Lookaheads and lookbehinds are zero-width assertions — they check whether a pattern exists ahead/behind the current position without consuming characters.

(?=...)   - Positive lookahead
(?!...)   - Negative lookahead
(?<=...)  - Positive lookbehind
(?<!...)  - Negative lookbehind

Positive Lookahead

Match X only if followed by Y:

// Match digits followed by 'px'
"16px 2em 24px".match(/\d+(?=px)/g); // ["16", "24"]

Negative Lookahead

Match X only if NOT followed by Y:

// Match .js files but not .json
/\.js(?!on)/.test("app.js");    // true
/\.js(?!on)/.test("data.json"); // false

Positive Lookbehind

Match X only if preceded by Y (ES2018+):

// Match numbers preceded by "$"
"$100 EUR200 $300".match(/(?<=\$)\d+/g); // ["100", "300"]

Negative Lookbehind

Match X only if NOT preceded by Y:

// Match 'ing' not preceded by 'think'
/(?<!think)ing/.test("singing");  // true
/(?<!think)ing/.test("thinking"); // false

Real-World Examples

Password validation with lookaheads:

// At least 8 chars, one uppercase, one digit
/^(?=.*[A-Z])(?=.*\d).{8,}$/.test("Password1"); // true

Extract prices:

const prices = "$9.99 and EUR14.50";
prices.match(/(?<=[$])\d+\.\d{2}/g); // ["9.99"]

Test all lookahead/lookbehind patterns in the Regex Tester.

Try These Tools

Regex Tester & Debugger

Test regular expressions against strings in real-time. Visualize matches, groups, and flags.

Related Articles

How to Write Efficient Regular Expressions

2 min read

Using Regex for Form Validation: Patterns and Pitfalls

2 min read

10 Regex Patterns Every Developer Should Know

1 min read

Back to Blog

Table of Contents

  • Table of Contents
  • What are Lookarounds?
  • Positive Lookahead
  • Negative Lookahead
  • Positive Lookbehind
  • Negative Lookbehind
  • Real-World Examples

Related Articles

How to Write Efficient Regular Expressions

2 min read

Using Regex for Form Validation: Patterns and Pitfalls

2 min read

10 Regex Patterns Every Developer Should Know

1 min read