JavaScript RegExp (?=...) Positive Lookahead

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is (?=...)?

(?=...) is a positive lookahead assertion. It succeeds only when the text right after the current position does match the inner pattern. It is zero-width—it peeks ahead without consuming characters or including them in the match.

Remember
(?=pattern)     → "followed by pattern"

/foo(?=bar)/
  "foobar" → "foo" ✓
  "foobaz" → null  ✗
  "foo"    → null  ✗

zero-width: match result never includes the lookahead text
contrast: (?!...) negative lookahead (must NOT follow)
UsePrefer
Match if suffix follows/foo(?=bar)/
http only in https/http(?=s)/
Must not follow(?!...)

How to Use (?=...)

Place the lookahead right after the text you want to keep.

  1. Write the part you want to match (e.g. foo or \d+).
  2. Add (?=required) immediately after it.
  3. Call test, match, or replace — the required text is checked but not captured.
  4. Stack several lookaheads at the start for multi-rule checks (e.g. password rules).
PieceRequired?Role
(?=…)YesAssert what must follow
Text before itUsualWhat actually gets matched / returned
Inner patternYesThe required following text

Pseudocode

Pseudocode
re = /token(?=good)/

find "token" in input
peek at the next characters (do not consume them)

if next characters match "good":
    succeed with match = "token" only
else:
    fail this attempt

Cheat Sheet

Keep these lookahead facts handy.

TopicDetail
Syntax(?=pattern)
MeaningFollowed by pattern
WidthZero-width (peek only)
vs (?!...)Negative: must not follow
Keep suffix out/\d+(?= dollars)/ → digits only
Not a quantifierAssertion, not repetition
StandardES3+ (universal)

Live Preview

Run common positive-lookahead patterns and see what matches.

Browser demo of (?=...) — same idea as the examples below.

Result
/foo(?=bar)/.exec("foobar")
→ "foo"

/* "bar" follows → lookahead passes */

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Match foo Followed by bar

Use (?=bar) to keep only the foobar case.

JavaScript
const words = ["foobar", "foobaz", "foo"];

for (const w of words) {
  console.log(w, w.match(/foo(?=bar)/));
}
// foobar ["foo"]
// foobaz null
// foo    null
Try It Yourself

How It Works

  1. Match the kept text. The engine first finds foo.
  2. Peek ahead. (?=bar) checks the next characters without consuming them.
  3. Allow foobar. When bar follows, the assertion passes → "foo".
  4. Reject the rest. foobaz and bare foo fail; bar is never part of the result.

Example 2 — Match http Only in https

Require the next character to be s with a one-letter lookahead.

JavaScript
const urls = ["http://a.com", "https://a.com"];

for (const url of urls) {
  console.log(url, url.match(/http(?=s)/));
}
// http://a.com  null
// https://a.com ["http"]
Try It Yourself

How It Works

  1. Match http. Both URLs start with those four letters.
  2. Require s. (?=s) passes only when the next character is s.
  3. http fails. Next character is :, not s → no match.
  4. https succeeds. Next is s → result is still only "http".

Example 3 — Numbers Followed by dollars

Keep only amounts that have the right unit after them.

JavaScript
const text = "5 dollars and 10 euros";

console.log(text.match(/\d+/g));               // ["5", "10"]
console.log(text.match(/\d+(?= dollars)/g));   // ["5"]
Try It Yourself

How It Works

  1. Plain digits. /\d+/g finds both 5 and 10.
  2. Add a filter. (?= dollars) keeps a number only if that phrase follows.
  3. Keep 5. After 5 comes dollars → assertion passes.
  4. Skip 10. After 10 comes euros → assertion fails.

Edge Cases & Pitfalls

Check these before relying on positive lookahead.

result

Lookahead text is never captured

/foo(?=bar)/ returns "foo", never "foobar". The peek is only a condition.

name

Not a quantifier

(?=...) does not repeat characters. For repetition use +, *, or ?.

stack

Stacking at the start

/^(?=.*[A-Z])(?=.*\d).+$/ runs several peeks from the same position before matching the rest.

contrast

Need the opposite? Use (?!...)

Negative lookahead (?!...) requires the following text not to match.

Browser & Runtime Support

Positive lookahead (?=...) is part of core JavaScript regular expression syntax (ES3+).

✓ Baseline · ES3+

RegExp (?=...) lookahead

Supported in every browser and Node.js version. No polyfill needed for positive lookahead assertions.

99% Universal syntax
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
(?=...) Excellent

Bottom line: Safe everywhere. Use (?=...) when you must require specific following text without consuming it.

Performance Notes

ApproachCostBest for
/token(?=good)/~match + peekLocal “followed by” rules
/^(?=.*[A-Z])(?=.*\d)/Extra scans per ruleShort form validation
Many stacked .* peeksCan growKeep short; test on real input

A simple peek is cheap. Stacked whole-string rules like (?=.*digit)(?=.*upper) re-scan the input—fine for passwords, avoid stacking many heavy lookaheads on huge strings.

Key Takeaways

  • Role: (?=...) means “followed by …”.
  • Zero-width: peeked text is not part of the match.
  • Opposite: (?!...) means “not followed by …”.
  • Not repetition: it is an assertion, not a quantifier like +.

One line: use (?=...) when you need a match only if something specific comes next.

Frequently Asked Questions

(?=...) is a positive lookahead assertion. It succeeds when the text immediately after the current position DOES match the pattern inside. It is zero-width—it checks ahead without consuming characters.
No. (?=...) is an assertion (a condition on what follows). Quantifiers like +, *, and ? repeat or optionalize the previous token. Lookahead never includes the peeked text in the match result.
(?=...) is positive lookahead—it requires the following text to match. (?!...) is negative lookahead—it requires the following text NOT to match. Both are zero-width.
Yes. Write /foo(?=bar)/. In the constructor use new RegExp('foo(?=bar)'). Positive lookahead has been supported since ES3.
Lookahead lets you require following text without including it in the match. /\d+(?= dollars)/ matches 5 in "5 dollars" but returns only "5", not "5 dollars".
Positive lookahead is core JavaScript regex syntax since ES3. It works in every browser and Node.js version.

Next: ? quantifier

Learn how ? makes the previous token optional (zero or one).

? quantifier tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful