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

Matching Words Preceded by Something

Using positive lookbehind to find patterns that appear after specific text without capturing the prefix.

Matching Words Preceded by Something

You'll learn how to use positive lookbehind to find patterns that come after specific text, without including that prefix in your match.

The Core Idea

Sometimes you want to match a word or pattern, but only if it appears after something specific. For example, you might want to find prices that come after a dollar sign, or usernames that follow an "@" symbol—but you don't want the "$" or "@" itself in your result.

Positive lookbehind lets you check what comes before your pattern without "consuming" that text. It's like saying: "Find me this word, but only if it's preceded by that other thing."

The Syntax

You use (?<=...) where the ... is the pattern that must appear before your main match:

const text = "Price: $50 and €30";
const regex = /(?<=\$)\d+/g;
const matches = text.match(regex);
console.log(matches); // ["50"] — not ["$50"]

Notice how the $50 becomes just 50—the dollar sign was required but not captured.

Real-World Example

Finding hashtag text without the "#":

const post = "Love #JavaScript and #Coding every day!";
const hashtagPattern = /(?<=#)\w+/g;
const tags = post.match(hashtagPattern);
console.log(tags); // ["JavaScript", "Coding"]

The # must be there, but it's not part of what you extract.

Why This Matters

Without lookbehind, you'd have to match both parts and then strip away what you don't want. Lookbehind keeps your matches clean from the start.

Key Takeaway: Positive lookbehind (?<=...) lets you match patterns that appear after specific text without including that prefix in your result—perfect for extracting values that follow markers like "$", "@", or "#".