The ? quantifier makes the previous token optional—it matches zero or one time. Use it for optional letters (https?), optional signs (-?\d+), optional prefixes, and flexible patterns without writing two separate regexes.
01
?
Zero or one
02
{0,1}
Same meaning
03
Optional
May skip
04
(grp)?
Whole group
05
Not (?=)
Quantifier
06
https?
Classic use
Fundamentals
Introduction
Real text is messy: URLs may use http or https, numbers may have an optional minus sign, and names may include an optional title. The ? quantifier handles all of these with one pattern by saying “this part may appear once, or not at all.”
Do not confuse ? the quantifier with ? in lookaheads like (?=...) or (?!...). When ? follows a character or group, it controls repetition. When it immediately follows ( at the start of a special construct, it begins a lookahead assertion—a different feature entirely.
Concept
Understanding the ? Quantifier
Place ? right after a token. The engine tries to match that token once; if that helps the overall pattern, it uses it. Otherwise it skips it (zero times) and continues. Both outcomes are valid.
In https?, the ? applies only to s—so s is optional. The pattern matches http and https but not ftp.
💡
Beginner Tip
To make several characters optional together, wrap them in a group: (abc)? not abc? (which only optionalizes c).
Usage
How to Use the ? Quantifier
Attach ? to single characters, escaped literals, or grouped sequences:
JavaScript
/https?/; // http or https
/-?\d+/; // optional minus on numbers
/colou?r/; // color or colour
/(Mr\. )?Smith/; // optional title
/(www\.)?example\.com/; // optional www.
"colour".match(/colou?r/); // ["colour"]
Combine with anchors for validation, or with the global flag to find every optional match in a string. Use ?? for the lazy form (prefer skipping when possible).
Foundation
📝 Syntax
JavaScript
token? // zero or one of token
https? // s is optional
(group)? // entire group optional
token{0,1} // equivalent to ?
token?? // lazy — prefer zero
Common patterns
/https?/ — match http or https.
/-?\d+/ — integers with optional leading minus.
/colou?r/ — American or British spelling.
/(www\.)?example\.com/ — domain with optional www..
/\.jpe?g$/i — .jpg or .jpeg extension.
Cheat Sheet
⚡ Quick Reference
Goal
Pattern / API
Optional character
s?
Optional group
(abc)?
Optional minus sign
-?\d+
Longhand form
{0,1}
Lazy optional
??
At most one
Unlike * (unlimited repeats)
Compare
📋 ? vs + vs *
Three repetition quantifiers and how many times the token may appear.
?
token?
Zero or one — optional
+
token+
One or more — required
*
token*
Zero or more — unlimited
On "a"
/a?/
Matches (zero times) — empty match
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it links. Examples cover optional protocol letters, spelling variants, signed numbers, optional groups, and file extensions.
See (x|y) alternation when optional choices are not simple zero-or-one.
Lazy quantifiers: +?, *?, and ?? prefer shorter matches.
Compatibility
Browser & Runtime Support
The ? quantifier is part of core JavaScript regular expression syntax.
✓ Baseline · ES3+
RegExp ? quantifier
Supported in every browser and Node.js version. Use ? to make tokens optional in patterns.
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 match zero or one of a token.
Wrap Up
Conclusion
The ? quantifier makes the previous token optional: zero or one occurrence. It powers familiar patterns like https? and -?\d+, and keeps regexes concise when a part of the input may or may not appear.
Remember to group multi-character optionals with parentheses, distinguish ? the quantifier from lookahead (?=...), and reach for * or + when you need more than one repetition. Next, learn the * quantifier.
Combine with anchors for strict validation patterns
Prefer https? over (http|https) when only one char differs
Test both present and absent cases for each optional part
❌ Don’t
Confuse ? quantifier with (?=...) lookahead
Write abc? when you mean (abc)?
Use ? when the token can repeat many times—use * or +
Forget that ? can match empty (zero times)
Overuse optionals when alternation lists are clearer
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about the RegExp ? quantifier
Make parts of your pattern optional with zero-or-one matching.
5
Core concepts
🔢01
?
Zero or one.
Syntax
📈02
{0,1}
Same rule.
Shorthand
📄03
(grp)?
Group optional.
Group
🔒04
https?
Classic.
Pattern
🚫05
Not (?=)
Lookahead.
Pitfall
❓ 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.
Did you know?
The pattern https? is one of the most copied regex snippets on the web. It works because ? binds tightly to the immediately preceding token—here, just the letter s—not to the entire word https.