Level 1: Foundations · Lesson 4 of 30
Quantifiers * + ?
Quantifiers say how many times the previous item may repeat. Learn the three basic ones: * for zero or more, + for one or more and ? for optional.
A quantifier is a symbol placed after a character or group that controls how many times it may appear. Without a quantifier, every item in a pattern must appear exactly once. With one, you can make items optional or repeatable. The three basic quantifiers are *, + and ?.
The star * means zero or more. The pattern go*gle matches ggle, gogle, google and gooogle, because the o may appear any number of times, including not at all. The star is powerful but easy to misuse, because zero is allowed: \d* can successfully match an empty string at every position.
The plus + means one or more. The pattern go+gle matches gogle and google but not ggle, because at least one o is required. When you want to grab a whole number, \d+ is the usual choice: it matches 7, 42 and 2024 as single matches rather than splitting them into separate digits.
The question mark ? means zero or one, in other words optional. The pattern colou?r matches both color and colour, but not colouur. It is perfect for small spelling differences, optional signs like a leading minus, or an optional hyphen in a code such as ID-?\d+.
A quantifier applies only to the item directly before it. In ab+, the plus repeats only b, so it matches ab, abb and abbb, not abab. To repeat several characters together, wrap them in a group like (ab)+, which you will learn in the groups lesson.
Quantifiers are greedy by default. They take as many characters as they can and only give some back if the rest of the pattern needs them. So .* on a long line grabs everything to the end of the line. Adding a ? after a quantifier, as in .*?, makes it lazy, taking as few characters as possible. Greedy and lazy behaviour work the same way in JavaScript, Python, PHP, Java, C#, Ruby, Go and Rust.
Practise by predicting matches before you run them. Write \d+ and \d* against the same text and compare the results. The difference between zero or more and one or more becomes obvious very quickly.
Live tester
2 matches (JavaScript engine)
color colour colouur
Examples
Numbers
/\d+/ga1 b22 c333
Matches: “1”, “22”, “333”
The plus keeps consecutive digits together, giving 1, 22 and 333.
Text
/go+gle/ggogle google gooogle ggle
Matches: “gogle”, “google”, “gooogle”
At least one o is required, so ggle is not matched.
Codes
/ID-?\d+/gID-12 ID34 ID-
Matches: “ID-12”, “ID34”
The optional hyphen allows both ID-12 and ID34, while ID- has no digits and fails.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Match both the American and British spelling of color, but not the typo with two u letters.
color colour colouur
Task 2: Match each whole number as one match, not as separate digits.
a1 b22 c333 d
Task 3: Match goal spelled with one or more o letters, but not gal.
goal gooal goooal gal