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
Global flag.g finds every signed or unsigned number.
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).
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
?Excellent
Bottom line: Safe everywhere. ? is the standard way to make a token optional.
Analysis
Performance Notes
Approach
Cost
Best for
/https?/
Trivial
Optional single characters
/-?\d+/g
Linear
Signed number extraction
Many nested optionals
Can grow
Keep 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.
Remember
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.