Concept
Quantifiers: *, +, ?, and {n,m}
Quantifiers repeat the preceding element. * is 0 or more, + is 1 or more, ? is 0 or 1. {n}, {n,}, and {n,m} give exact counts and ranges.
The four shorthand quantifiers
* = zero or more. + = one or more. ? = zero or one (makes the element optional). These apply to the immediately preceding token — a single character, a character class, or a group.
Explicit count with braces
{n} matches exactly n times. {n,} matches n or more. {n,m} matches between n and m times, inclusive. Use these when you need precise length control, like 'exactly 4 digits' for a year.
Grouping matters
Quantifiers apply to the preceding token, not the whole sequence. (ha)+ matches ha, haha, hahaha — wrap multiple characters in parentheses to quantify them as a unit. Without the group, ha+ would match h followed by one or more a's.
Quantifiers are greedy by default
+ and * will consume as much as possible and back off only if the overall match would otherwise fail. See the lazy vs greedy reference for when to add a ? suffix like +? or {n,m}? for minimal matching.
Related patterns
Email Address Validation
Match and validate email addresses in the standard user@domain.tld format.
/[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+…/gUS Phone Number
Match US phone numbers in common formats: (555) 867-5309, 555-867-5309, 5558675309.
/\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d…/gUUID (v1–v5)
Match RFC 4122 UUIDs (versions 1–5) in the standard 8-4-4-4-12 hex format.
/[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a…/giInteger
Matches whole integers, including negative numbers.
/^-?\d+$/