Back to JavaScript Notes
Topic #344

Quantifiers

You will be able to use JavaScript regular expression quantifiers to specify how many times a character, group, or other token may repeat.

What it is

A quantifier is a regular expression operator that follows a token and controls repetition. A token can be a literal character, a character class such as [0-9], a dot, or a group such as (ab). In JavaScript, common quantifiers are ? for zero or one, * for zero or more, + for one or more, {n} for exactly n, {n,} for at least n, and {n,m} for between n and m times. Quantifiers are greedy by default, meaning they try to match as much as possible. Adding ? after a quantifier makes it lazy, so it tries to match as little as possible.

Why it matters

  • It lets you validate bounded input, such as phone digits, ZIP codes, or password lengths.
  • It reduces verbose patterns; /a{3}/ is clearer than /aaa/.
  • It helps match repeated structures, such as whitespace, digits, or optional prefixes.
  • It gives precise control over how much text a pattern may consume.
  • It supports both greedy and lazy matching when you need the longest or shortest possible match.

Syntax or steps

Place the quantifier immediately after the token you want to repeat. If you need to repeat more than one character, wrap those characters in a group.

token{min,max}
group{min,max}
token{min,max}?

For example, /a{2,4}/ repeats the letter a between 2 and 4 times, while /(ab){2,4}/ repeats the pair ab between 2 and 4 times.

Example

const bounded = /a{2,4}/;
const lazy = /a{2,4}?/;

const tests = ["a", "aa", "aaa", "aaaa", "aaaaa"];

for (const text of tests) {
  console.log(text, bounded.test(text));
}

console.log("aaaa".match(lazy));

Part-by-part

  • /a{2,4}/ matches any substring containing 2, 3, or 4 consecutive a characters.
  • bounded.test("a") is false because there is only one a.
  • bounded.test("aa"), bounded.test("aaa"), and bounded.test("aaaa") are true.
  • bounded.test("aaaaa") is also true because the pattern can match a substring of four a characters.
  • /a{2,4}?/ is lazy, so in "aaaa" it matches the shortest allowed run: "aa".

Common mistakes

  • Repeating only the previous token: /ab{2,4}/ repeats b, not ab. Fix it with /(ab){2,4}/.
  • Forgetting that quantifiers match substrings unless anchored. /a{2,4}/ matches inside "baaab". Use /^a{2,4}$/ if the whole string must be only 2 to 4 a characters.
  • Assuming greedy is always desired. /a{2,4}/ prefers four a characters when available. Use /a{2,4}?/ when you want the shortest match.
  • Using an invalid range such as {2,1}. The minimum must be less than or equal to the maximum.

When to use it

Use bounded quantifiers when the number of repetitions matters. Use simpler quantifiers when exact limits are unnecessary.

NeedUseExample
Optional token?/colou?r/
Zero or more*/a*/
One or more+/a+/
Exact range{n,m}/a{2,4}/

Practice

  1. Guided: Write a pattern that matches a ZIP code with 5 digits, optionally followed by a hyphen and 4 more digits. Use /\d{5}(-\d{4})?/. Expected: "90210" matches, "90210-1234" matches, and "9021" does not.
  2. Challenge: Write a pattern that matches a repeated word, such as "the the". Hint: capture one word, require whitespace, then use a backreference: /\b(\w+)\s+\1\b/.

Quick check

Question: What does /a{2,4}/ match?

Answer: It matches a substring containing 2, 3, or 4 consecutive a characters. By default it is greedy, so it prefers the longest allowed run.

Summary

Quantifiers turn a single regex token into a repetition rule. In JavaScript, choose ?, *, +, or {n,m} based on whether the token is optional, unlimited, required, or bounded. Remember to group tokens when you need to repeat more than one character.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Quantifiers – FAQs

Quick answers about learning Quantifiers in JavaScript.

This free note from Coding Now Tech Institute explains Quantifiers in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including Quantifiers, is 100% free with no signup required.
With focused practice, most students grasp Quantifiers in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now