How-to

How to Match an Optional Group

Put a ? after a character, group, or character class to make it optional. Groups with ? match zero or one occurrence of the whole group.

? after a single character

The simplest case — colou?r matches both 'color' and 'colour.' The ? makes the preceding u optional. Works the same for character classes: ab[cd]?e matches 'abe,' 'abce,' or 'abde.'

? after a group

Parenthesize first, then add ?. The entire group becomes optional. A ZIP+4 pattern is the canonical example: the base 5 digits are required, and the +4 extension is an optional -#### group.

ZIP code with optional +4

Try this
/^\d{5}(-\d{4})?$/

Input

90210-1234

Result

Match (captured -1234)

Same regex, base-only input

Try this
/^\d{5}(-\d{4})?$/

Input

90210

Result

Match (no +4 captured)

Non-capturing optional groups

If you don't need the submatch, use (?:...)? instead of (...)?. Slightly faster and keeps your $1 / $2 references aligned to the groups you actually care about.

Protocol optional, non-capturing

Try this
/(?:https?://)?www\.example\.com/

Input

www.example.com

Result

Match

Multiple optional pieces

Stack them as needed: (?:\+1[\s-]?)?\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4} accepts US phone numbers with optional country code, optional parens around area code, and any of space/hyphen/nothing as separators. Each ? independently controls its own piece.

Related patterns

All reference guidesOpen the RegexPro tester →