Course contentsShow
← JavaScript
Lesson 1714 of 2,72336. Regular ExpressionsFree lesson

Positive Lookbehind Syntax (?<=...)

Using positive lookbehind to assert that a pattern must appear behind without including it in the match.

Positive Lookbehind Syntax (?<=...)

You'll learn how to assert that a pattern must appear before your match without including that preceding text in the result.

What Is Positive Lookbehind?

Positive lookbehind is the mirror image of positive lookahead. While lookahead checks what comes after your match, lookbehind checks what comes before. The syntax (?<=...) lets you say "match this pattern, but only if it's preceded by that pattern" — and the preceding part won't be captured.

Think of it like requiring a password before entry: you need to show the password, but it doesn't become part of what you're taking inside.

How It Works

const price = "The item costs $50";
const dollars = price.match(/(?<=\$)\d+/);
console.log(dollars[0]); // "50"

Here, (?<=\$) means "only match digits that come after a dollar sign," but the $ itself isn't included in the match.

Compare with a normal pattern:

const withDollar = price.match(/\$\d+/);
console.log(withDollar[0]); // "$50" — includes the $

Practical Example

const text = "user:john, admin:alice";
const admins = text.match(/(?<=admin:)\w+/);
console.log(admins[0]); // "alice"

The pattern matches word characters, but only when they're preceded by admin:. The prefix itself is excluded from the result.

When to Use It

Use positive lookbehind when you need context before your match but don't want that context in your result — extracting values after labels, parsing formatted text, or validating position-dependent patterns.

Key Takeaway: Positive lookbehind (?<=...) asserts that a pattern must appear before your match without including it in the result — perfect for extracting values that follow specific prefixes.