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.

* (zero or more)

Try this
/ab*c/

Input

ac, abc, abbbbc

Result

Matches: ac, abc, abbbbc

+ (one or more)

Try this
/ab+c/

Input

ac, abc, abbbbc

Result

Matches: abc, abbbbc (ac fails)

? (zero or one)

Try this
/colou?r/

Input

color, colour

Result

Matches: color, colour

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.

Exactly 4 digits

Try this
/\d{4}/

Input

2026 is the year

Result

Match: 2026

Between 3 and 5 word chars

Try this
/\w{3,5}/

Input

one two three

Result

Matches: one, two, three

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

All reference guidesOpen the RegexPro tester →