Visual regex workbench

Build, test, explain and master regex with visual tools, guides, examples and a growing regex library.

Regex guide

Regex Lookarounds

Lookarounds are zero-width assertions that verify whether a pattern exists before or after the current position without consuming any characters. They are powerful tools for validation, filtering and advanced matching.

Why lookarounds matter

Lookarounds allow a regular expression to verify surrounding text without making it part of the final match.

This makes complex validation and filtering possible while keeping the matched result clean.

Although they can seem intimidating at first, lookarounds often produce simpler and more maintainable regexes than alternative approaches.

Lookarounds do not consume characters

Unlike character classes or groups, lookarounds never consume any characters.

They simply verify whether a condition is true before or after the current position.

Understanding this zero-width behavior is the key to mastering lookarounds.

Lookaheads versus lookbehinds

Lookaheads inspect what comes after the current position, while lookbehinds inspect what comes before it.

Both perform checks without changing what is actually matched.

Choosing the right assertion depends entirely on where the required context is located.

Lookarounds simplify validation

Lookarounds are frequently used to validate multiple independent conditions within a single regular expression.

Password validation is one of the best-known examples, where uppercase letters, digits and special characters can all be required without consuming them individually.

This approach keeps validation rules expressive while avoiding duplicated patterns.

Common lookaround mistakes

One common mistake is expecting a lookaround to become part of the matched text.

Another is forgetting that some regex engines have restrictions on lookbehind support or variable-length lookbehinds.

Testing your regex in the target engine is especially important when using advanced assertions.

Lookarounds in real-world regexes

Lookarounds appear in password validation, currency extraction, log analysis, syntax highlighting and many parsing tasks.

They allow conditions to be expressed without making the overall pattern significantly longer.

Once mastered, lookarounds often replace much more complicated regex constructions.

Continue learning

πŸ“˜ Recommended guide

JavaScript Lookbehind

JavaScript lookbehind deserves its own guide because browser support arrived much later than lookahead.

Learn the JavaScript-specific syntax, compatibility and practical workarounds.

Read the complete guide β†’

Positive lookahead

A positive lookahead checks that another pattern follows the current position.

The following text must be present, but it is not included in the final match.

\b\w+(?=:)
Quick test

Negative lookahead

A negative lookahead checks that another pattern does not follow the current position.

It is commonly used to exclude unwanted suffixes, extensions or formats.

\b\w+\b(?!:)
Quick test

Positive lookbehind

A positive lookbehind checks what appears immediately before the current position.

The preceding text is required, but it is not included in the final match.

(?<=\$)\d+
Quick test

Negative lookbehind

A negative lookbehind checks that a pattern does not appear immediately before the current position.

It is useful for excluding values with a particular prefix or symbol.

(?<!\$)\b\d+\b
Quick test

Multiple lookaheads

Several lookaheads can be chained to verify independent conditions.

This technique is frequently used for password policies requiring uppercase letters, digits and special characters.

^(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*]).+$
Quick test

Zero-width assertions

Lookarounds are called zero-width assertions because they verify a condition without consuming characters.

Only the part outside the assertion becomes part of the final match.

cat(?=fish)
Quick test

Combining lookahead and lookbehind

Lookaheads and lookbehinds can be combined around the same match.

This allows you to extract a value only when both its prefix and suffix satisfy specific conditions.

(?<=\$)\d+(?= USD)
Quick test

Lookarounds with groups and alternation

Groups and alternation can be used inside a lookaround to accept several possible contexts.

This keeps the surrounding text outside the match while allowing more flexible conditions.

(?<=\b(?:Mr|Ms)\s)\w+
Quick test