Regex Positive Lookahead – Match Before Following Text

Local only

Use positive lookahead when a token should match only if specific text follows, while leaving the following text outside the match.

14 chars · 1 line

8 chars · 1 line

Pattern did not match the inputFlags: g

Regex lookahead behavior

How to use positive lookahead in JavaScript regex

Positive lookahead (?=...) checks what comes next without consuming it. The preloaded pattern returns 42 because it is followed by USD, while the USD text itself is not part of the match. This tool is free to use. No account or payment is required.

Last updated

What you can do here

  • Positive lookahead example
  • Non-consuming assertion
  • Match All output
  • JavaScript RegExp semantics
  • Context-sensitive matching
  • Browser-local processing

Steps

  1. Write the token that should be returned by the match.
  2. Place the required following context inside (?=...).
  3. Use Match All against inputs with allowed and disallowed following text to verify the assertion boundary.

Lookahead asserts context without moving the match endpoint

A successful lookahead can inspect characters after the current position, but those asserted characters are not consumed into the complete match. This is useful when context controls eligibility but should remain outside the extracted value.

Positive lookahead example

Match a number only when USD follows.

Pattern and input

\d+(?= USD) / g
42 USD, 19 EUR

Matched text

42

Common questions

Frequently asked questions

Is the lookahead text included in the match?

No. Positive lookahead checks the following context without consuming it into the complete match.

Can lookahead contain a more complex pattern?

Yes. The assertion can contain regex syntax supported by JavaScript, subject to the usual performance and compatibility considerations.

What is the opposite of positive lookahead?

Negative lookahead (?!...) succeeds when the specified following pattern does not match.

Debugging a specific Regex issue? Browse Regex troubleshooting.