Visual regex workbench

Build, import, explain, test and share regex visually.

Regex guide

JavaScript Regex Lookbehind

Lookbehind lets a regex check what appears before the current position without including it in the final match.

What is a lookbehind assertion?

A lookbehind assertion allows a regex to verify what appears immediately before a match without including those characters in the result.

Unlike normal groups, lookbehinds are zero-width assertions, meaning they do not consume any characters from the matched text.

Positive vs negative lookbehind

A positive lookbehind succeeds only when a specific pattern appears before the current position.

A negative lookbehind does the opposite and succeeds only when a pattern is not present before the match.

Why use lookbehind in JavaScript?

Lookbehind assertions can make regex patterns shorter, cleaner and easier to understand.

They are especially useful when extracting values that must be preceded by a specific prefix, currency symbol, keyword or delimiter.

Common lookbehind use cases

Developers frequently use lookbehind assertions to extract prices, IDs, measurements, file extensions and values that appear after a known label.

They can also simplify data extraction tasks that would otherwise require additional string manipulation.

Browser compatibility considerations

Modern JavaScript engines support lookbehind assertions, but older browsers and legacy environments may not.

When compatibility is important, you should verify support or rewrite the pattern using capturing groups.

Lookbehind vs capturing groups

Many tasks can be solved using either lookbehind assertions or capturing groups.

Lookbehind often produces cleaner matches because the prefix remains outside the returned result.

When not to use lookbehind

If maximum compatibility is required, capturing groups may be a safer choice.

Lookbehind assertions should improve readability, not make a regex unnecessarily complex.

Positive lookbehind

A positive lookbehind succeeds only if the previous text matches the expression inside the lookbehind.

For example, (?<=€)\d+ matches digits preceded by the euro symbol.

(?<=€)\d+
Quick test

Negative lookbehind

A negative lookbehind succeeds only if the previous text does not match the expression.

For example, (?<!-)\d+ can be used to avoid numbers preceded by a minus sign.

(?<!-)\d+
Quick test

Compatibility

Modern JavaScript engines support lookbehind, but older browsers may not.

If compatibility matters, consider rewriting the pattern without lookbehind.