JavaScript RegExp ? Quantifier

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

What Is ??

The ? quantifier means zero or one of the previous token. It makes that token optional: present once, or missing. Classic uses include https? (http or https) and -?\d+ (optional minus before digits).

Remember
token?     → zero or one of token  (same as {0,1})

/https?/  on "http"   → "http"
/https?/  on "https"  → "https"
/colou?r/ on "color"  → "color"
/colou?r/ on "colour" → "colour"

not the same as (?=...) lookahead — context matters
UsePrefer
Optional letter/https?/
Optional sign/-?\d+/
Zero or more?* instead

How to Use ?

Place ? right after the token you want to make optional.

  1. Choose the token: a character, class, or group.
  2. Add ? immediately after it (e.g. s? or (Mr\. )?).
  3. Remember: at most one match — for many repeats use * or +.
  4. Do not confuse with (?=...) — that ? starts a lookahead, not optional matching.
PieceRequired?Role
Preceding tokenYesWhat may appear 0 or 1 times
?YesZero or one (greedy)
??OptionalZero or one (lazy — prefer 0)

Pseudocode

Pseudocode
re = /token?/

if next text matches "token":
    take it once (greedy prefers yes)
else:
    take nothing — still OK

never take "token" more than once

Cheat Sheet

Keep these ? facts handy.

TopicDetail
MeaningZero or one of previous token
Shorthand? = {0,1}
vs ** = zero or more
vs ++ = one or more (required)
Groups(abc)? makes the whole group optional
Not lookahead(?=...) is a different use of ?
StandardES3+ (universal)

Live Preview

Run common ? patterns and see what matches.

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

Result
/https?/.exec("http")
→ "http"

/* s? is optional — zero s is fine */

JavaScript Examples

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

Example 1 — Match http or https

Make the final s optional with https?.

JavaScript
const schemes = ["http", "https", "ftp"];

for (const s of schemes) {
  console.log(s, s.match(/https?/));
}
// http  ["http"]
// https ["https"]
// ftp   null
Try It Yourself

How It Works

  1. Fixed prefix. The pattern always needs http.
  2. Optional s. s? allows one s or none.
  3. Both schemes pass. http and https match.
  4. Other schemes fail. ftp does not start with http.

Example 2 — American or British Spelling

Allow an optional u in colou?r.

JavaScript
const words = ["color", "colour", "colar"];

for (const w of words) {
  console.log(w, /colou?r/.test(w));
}
// color  true
// colour true
// colar  false
Try It Yourself

How It Works

  1. Shared letters. Both spellings share colo … r.
  2. Optional u. u? accepts one u or skips it.
  3. Both spellings pass. color and colour succeed.
  4. Wrong letter fails. colar has a, not optional u.

Example 3 — Optional Minus with -?\d+

Match positive and negative integers in one pattern.

JavaScript
const text = "scores: 12, -3, and 0";

console.log(text.match(/-?\d+/g));
// ["12", "-3", "0"]
Try It Yourself

How It Works

  1. Optional minus. -? takes a leading - if present.
  2. Then digits. \d+ requires at least one digit.
  3. Global flag. g finds every signed or unsigned number.
  4. All three match. 12, -3, and 0 are captured.

Edge Cases & Pitfalls

Check these before relying on ?.

scope

Applies to one token only

ab? makes only b optional. Use (ab)? when the whole pair may be missing.

context

Not the same as (?=...)

After (, ? can start a lookahead. After a token, ? means optional. See positive lookahead.

count

At most one — not many

For zero or more repeats use *. For one or more use +.

literal

Matching a literal ?

Escape it: /\?/. Unescaped ? is always a quantifier (or part of an assertion).

Browser & Runtime Support

The ? quantifier is part of core JavaScript regular expression syntax (ES3+).

✓ Baseline · ES3+

RegExp ? quantifier

Supported in every browser and Node.js version. Use ? for optional (zero-or-one) tokens and groups.

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. ? is the standard way to make a token optional.

Performance Notes

ApproachCostBest for
/https?/TrivialOptional single characters
/-?\d+/gLinearSigned number extraction
Many nested optionalsCan growKeep patterns simple

A single ? is cheap. Nested optional groups on long strings can create extra backtracking paths—prefer clear, short patterns for beginners and production alike.

Key Takeaways

  • Role: ? means zero or one of the previous token.
  • Shorthand: ? equals {0,1}.
  • Groups: (prefix)? makes a whole chunk optional.
  • Context: after (, ? may start a lookahead — not optional matching.

One line: use ? when a character or group may appear once or not at all.

Frequently Asked Questions

The ? quantifier means zero or one of the preceding token. It makes that token optional. For example, https? matches both http and https because the s is optional.
Yes. ? is shorthand for {0,1}—the preceding item may appear once or not at all. Both are greedy by default in JavaScript.
After a token or group, ? is a quantifier (optional repetition). After an opening parenthesis at the start of a group like (?=...), ? begins a lookahead assertion—not optional matching. Context determines the meaning.
? allows zero or one repetition. * allows zero or more. Use ? when something may appear at most once; use * when it can repeat many times.
Yes. (abc)? makes the entire group optional—it matches once or skips it entirely. Example: (Mr\. )?Smith matches Smith and Mr. Smith.
The ? quantifier is core JavaScript regex syntax since ES3. It works in every browser and Node.js version without polyfills.

Next: * quantifier

Learn how * matches zero or more of the previous token.

* 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.

10 people found this page helpful