← Back to Blog

Regex Lookahead and Lookbehind: A Visual Guide

May 16, 2026 3 min read By CodeTidy Team

The Regex Riddle: Cracking the Code with Lookahead and Lookbehind

We've all been there - staring at a regular expression, wondering why it's not matching what we want it to. One of the most powerful, yet often misunderstood, aspects of regex is lookahead and lookbehind assertions. In this article, we'll delve into the world of zero-width assertions and explore how to use them to solve real-world problems.

Table of Contents

  • Understanding Lookahead and Lookbehind
  • Positive and Negative Lookahead
  • Positive and Negative Lookbehind
  • Practical Examples
  • Browser Support and Limitations
  • Key Takeaways
  • FAQ

Understanding Lookahead and Lookbehind

Lookahead and lookbehind assertions are a type of zero-width assertion, meaning they don't consume any characters in the string being matched. They allow us to peek ahead or behind the current position in the string, without including those characters in the match. This is incredibly useful for validating input, extracting data, and more.

Think of lookahead as "look ahead and see if this condition is true, but don't include it in the match." Conversely, lookbehind is "look behind and see if this condition is true, but don't include it in the match."

Positive and Negative Lookahead

Positive lookahead is denoted by (?=pattern), while negative lookahead is denoted by (?!pattern). Let's consider an example:

const string = "Hello, world!";
const regex = /Hello(?=, world!)/;
console.log(string.match(regex)); // Output: ["Hello"]

In this example, the regex matches "Hello" only if it's followed by ", world!". The lookahead assertion doesn't include the ", world!" in the match.

Negative lookahead is useful when we want to match something only if it's not followed by a certain pattern:

const string = "Hello, world!";
const regex = /Hello(?!world)/;
console.log(string.match(regex)); // Output: null

Positive and Negative Lookbehind

Positive lookbehind is denoted by (?<=pattern), while negative lookbehind is denoted by (?<!pattern). Let's consider an example:

const string = "Hello, world!";
const regex = /(?<=Hello, )world/;
console.log(string.match(regex)); // Output: ["world"]

In this example, the regex matches "world" only if it's preceded by "Hello, ". The lookbehind assertion doesn't include the "Hello, " in the match.

Negative lookbehind is useful when we want to match something only if it's not preceded by a certain pattern:

const string = "Hello, world!";
const regex = /world(?<!Hello, )/;
console.log(string.match(regex)); // Output: null

Practical Examples

  1. Email validation: Use a lookahead to ensure an email address has a specific domain:
const email = "john.doe@example.com";
const regex = /^[a-zA-Z0-9._%+-]+(?=@example\.com)[a-zA-Z0-9._%+-]+$/;
console.log(email.match(regex)); // Output: ["john.doe@example.com"]
  1. Password validation: Use a lookbehind to ensure a password has at least one uppercase letter:
const password = "HelloWorld!";
const regex = /^(?=.*[A-Z]).*$/;
console.log(password.match(regex)); // Output: ["HelloWorld!"]

Browser Support and Limitations

Lookahead and lookbehind assertions are supported in most modern browsers, including Chrome, Firefox, and Edge. However, older browsers like Internet Explorer may not support these features.

Key Takeaways

  • Lookahead and lookbehind assertions are zero-width assertions that allow us to peek ahead or behind the current position in a string.
  • Positive lookahead and lookbehind are denoted by (?=pattern) and (?<=pattern), respectively.
  • Negative lookahead and lookbehind are denoted by (?!pattern) and (?<!pattern), respectively.
  • Use lookahead and lookbehind assertions to validate input, extract data, and more.

FAQ

Q: What's the difference between lookahead and lookbehind?

A: Lookahead peeks ahead in the string, while lookbehind peeks behind.

Q: Can I use lookahead and lookbehind together?

A: Yes, you can use multiple lookahead and lookbehind assertions in a single regex pattern.

Q: Are lookahead and lookbehind assertions supported in all browsers?

A: Most modern browsers support lookahead and lookbehind assertions, but older browsers like Internet Explorer may not.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp