JavaScript RegExp ? Quantifier
Last Updated :
05 Aug, 2025
The ? quantifier in JavaScript regular expressions specifies that the preceding element is optional. It matches zero or one occurrence of the element, making it a useful tool for flexible pattern matching.
JavaScript
let regex = /colou?r/;
let str1 = "color";
let str2 = "colour";
let str3 = "colouur";
console.log(regex.test(str1));
console.log(regex.test(str2));
console.log(regex.test(str3));
The pattern /colou?r/ matches both "color" and "colour" because the u is optional. It does not match "colouur" because it has more than one u.
Syntax:
element?
- element: The character, group, or character class to be made optional.
Key Points
- Matches zero or one occurrence of the preceding element.
- Useful for optional letters or segments in words.
- Can apply to individual characters or grouped patterns.
Real-World Examples
1. Optional Characters in Words
JavaScript
let regex = /behaviou?r/;
console.log(regex.test("behavior"));
console.log(regex.test("behaviour"));
console.log(regex.test("behaviours"));
Matches "behavior" and "behaviour" but not "behaviours".
2. Optional Groups
JavaScript
let regex = /(?:https?:\/\/)?www\.\w+\.\w+/;
console.log(regex.test("https://www.example.com/"));
console.log(regex.test("www.example.com"));
console.log(regex.test("example.com"));
The https?:// group is optional, so the pattern matches URLs with or without "https://" or "https://".
3. Matching Optional Digits
JavaScript
let regex = /\d{3}-?\d{4}/;
console.log(regex.test("123-4567"));
console.log(regex.test("1234567"));
console.log(regex.test("123--4567"));
The - is optional, so the pattern matches phone numbers with or without a hyphen.
4. Making a Case-Insensitive Match
JavaScript
let regex = /[A-Z]?pple/i;
console.log(regex.test("Apple"));
console.log(regex.test("pple"));
console.log(regex.test("aPple"));
The optional [A-Z] allows for matching "Apple" or "pple".
5. Validating Postal Codes
JavaScript
let regex = /\d{5}(-\d{4})?/;
console.log(regex.test("12345"));
console.log(regex.test("12345-6789"));
console.log(regex.test("123456789"));
Matches U.S. ZIP codes with or without the four-digit extension.
Common Patterns Using ?
- Optional Characters in Words:
/colou?r/
/(?:https?:\/\/)?www\.\w+\.\w+/
- Phone Numbers with Optional Separators:
/\d{3}-?\d{4}/
- Case Insensitivity for a Letter:
/[A-Z]?pple/i
- Postal Codes with Optional Extensions:
/\d{5}(-\d{4})?/
Limitations
- Matches Only Once: Unlike the * quantifier (zero or more) or + (one or more), ? matches at most one occurrence.
- Limited for Repeating Patterns: Use other quantifiers (*, +, {}) for patterns that require multiple matches.
Why Use the ? Quantifier?
- Flexibility: Handles optional elements gracefully.
- Readability: Keeps patterns concise and easy to understand.
- Real-World Use Cases: Essential for handling variable text structures like optional URL protocols, postal code extensions, or flexible word spellings.
Conclusion
The ? quantifier is a powerful and versatile tool for making elements optional in regular expressions, enabling flexible and precise pattern matching.
Recommended Links:
Explore
JavaScript Basics
Array & String
Function & Object
OOP
Asynchronous JavaScript
Exception Handling
DOM
Advanced Topics