JavaScript RegExp {n} Exact Quantifier

Beginner
7 min read
Updated: Oct 2026
3 examples
Live preview

What Is the {n} Quantifier?

Place {n} after a token to match it exactly n times — no fewer, no more for that run. Ideal for fixed-width fields like \d{4} (year) or \d{5} (ZIP shape).

Remember
token{n}     → exactly n of token
\d{3}        → "123" ✓ · "12" ✗ · "1234" still finds "123"
(ab){2}      → "abab" ✓

{3} exact    ·  {3,} three or more  ·  {2,4} between 2 and 4
^\d{5}$      → whole string is exactly five digits
UsePrefer
Exactly n timestoken{n}
n or moretoken{n,}
Between n and mtoken{n,m}

How to Use {n}

Attach {n} to the token whose length must be fixed.

  1. Choose the token (character, class, or group) — e.g. \d, a, (ha).
  2. Add {n} with the exact count — e.g. \d{3}, (ha){2}.
  3. Add surrounding literals or separators when building dates, IDs, or codes.
  4. Use ^ and $ when the entire string must be that exact length.
PieceRequired?Role
Preceding tokenYesWhat repeats
{n}YesExact repetition count
^ / $OptionalNeeded for whole-string length checks

Pseudocode

Pseudocode
pattern = <token>{n}

count = 0
while count < n:
    if next input matches <token>:
        count = count + 1
    else:
        fail

// exactly n succeeded — do not require a (n+1)th match
continue with rest of pattern

Cheat Sheet

Keep these facts handy while writing fixed-count patterns.

TopicDetail
MeaningExactly n of the previous token
Digits\d{3}, \d{4}, \d{5}
Group(ab){2} → abab
Date shape\d{4}-\d{2}-\d{2}
Full field/^\d{5}$/
Family{n} · {n,} · {n,m}
StandardES3+ (universal)

Live Preview

Pick an exact-count pattern and a sample string to see test() / match() results.

Browser demo of the {n} exact quantifier — same idea as the examples below.

Result
pattern = /\d{3}/
text    = "12"
test()  → false
match() → null

/* need exactly three digits */

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Match Exactly Three Digits with \d{3}

Find a run of exactly three digits inside different strings.

JavaScript
const samples = ["12", "123", "1234", "abc123def"];

samples.forEach((s) => {
  console.log(s, s.match(/\d{3}/));
});
Try It Yourself

How It Works

  1. Exact count. \d{3} needs three digits in a row — no fewer.
  2. Too short fails. "12" has only two digits → null.
  3. Longer still matches a run. "1234" yields the first three digits "123".
  4. Search, not whole string. Without ^$, the engine finds the first valid run anywhere.

Example 2 — Repeat a Group Exactly with (ha){3}

Match hahaha by repeating the ha pair three times.

JavaScript
const words = ["hahaha", "haha", "hahahaha"];

words.forEach((w) => {
  console.log(w, /(ha){3}/.test(w));
});
Try It Yourself

How It Works

  1. Group first. Parentheses make ha one token.
  2. Count units, not letters. {3} means three ha pairs → hahaha.
  3. Two is not enough. "haha" is only (ha){2} → false.
  4. Substring OK. "hahahaha" contains hahaha, so test still returns true.

Example 3 — Validate a Five-Digit Field with ^\d{5}$

Require the entire value to be exactly five digits using anchors.

JavaScript
const rule = /^\d{5}$/;

console.log(rule.test("90210"));   // true
console.log(rule.test("9021"));    // false — too short
console.log(rule.test("902101"));  // false — too long
console.log(rule.test("9021a"));   // false — not all digits
Try It Yourself

How It Works

  1. Exact length. \d{5} requires five digits in a row.
  2. Pin both ends. ^ and $ reject anything before or after.
  3. Short and long fail. Four or six digits both miss the exact count.
  4. Why anchors matter. Without them, \d{5} would match five digits inside longer text.

Edge Cases & Pitfalls

Check these before relying on {n} in real projects.

partial

Without anchors, longer strings can still match

/\d{3}/ matches inside "1234". Use /^\d{3}$/ when the whole value must be exactly three digits.

scope

{n} applies only to the previous token

ab{2} is a + bb. Use (ab){2} when the whole pair must repeat.

choice

Exact vs range

Need “at least three”? Use {3,}. Need “two to four”? Use {2,4}.

shape

Shape is not calendar validity

\d{4}-\d{2}-\d{2} accepts 2024-99-99. Exact counts check width, not real dates.

Browser & Runtime Support

The {n} exact quantifier is part of core JavaScript regex (ES3+). It works everywhere without polyfills.

✓ Baseline · ES3+

RegExp {n} quantifier

Supported in every modern and legacy browser, plus all Node.js versions. Universal syntax for exact repetition.

99% Universal syntax
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
{n} Excellent

Bottom line: Safe everywhere. Use {n} whenever a token must repeat a fixed number of times.

Performance Notes

Pattern styleCostBest for
\d{n} / \w{n}CheapFixed-width fields
^…{n}$Cheap + early failWhole-string validation
Huge n on wildcardsCan growPrefer specific classes over .{n}

Exact counts on simple tokens are fast and predictable. Prefer \d{n} over open .{n} when you know the character class, and anchor when validating whole fields.

Key Takeaways

  • Role: {n} means exactly n of the previous token.
  • Groups: (ab){2} repeats the whole pair, not just b.
  • Validation: pair with ^ and $ for full-field length.
  • Family: use {n,} or {n,m} when exact is too strict.

One line: {n} locks the count — perfect for fixed-width runs, and strongest when anchored for whole-field checks.

Frequently Asked Questions

The {n} quantifier means exactly n repetitions of the preceding token. Example: \d{4} matches exactly four digits in a row. n must be a non-negative integer.
{3} matches exactly three repetitions—no more, no fewer. + matches one or more with no upper limit. So /a{3}/ matches only aaa as a three-a run, while /a+/ also matches a, aa, aaaa, and so on.
Yes. (ab){2} matches ab exactly twice: abab. The quantifier counts full group repetitions, not individual characters inside the group.
No. Without anchors, {n} can match a run inside a longer string. Use ^ and $ (for example /^\d{5}$/) when the entire value must be exactly n of that token.
{n} is exact. {n,} means n or more. {n,m} means between n and m inclusive. Use the form that matches your length rule.
The {n} quantifier is core JavaScript regex syntax since ES3. It works in every browser and Node.js version without polyfills.

Next: {n,} quantifier

Learn how to require at least n repetitions with no upper limit.

{n,} tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful