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
Attach {n} to the token whose length must be fixed.
Choose the token (character, class, or group) — e.g. \d, a, (ha).
Add {n} with the exact count — e.g. \d{3}, (ha){2}.
Add surrounding literals or separators when building dates, IDs, or codes.
Use ^ and $ when the entire string must be that exact length.
Piece
Required?
Role
Preceding token
Yes
What repeats
{n}
Yes
Exact repetition count
^ / $
Optional
Needed 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
Reference
Cheat Sheet
Keep these facts handy while writing fixed-count patterns.
Topic
Detail
Meaning
Exactly 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}
Standard
ES3+ (universal)
Try it
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 */
Code
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.
Count units, not letters.{3} means three ha pairs → hahaha.
Two is not enough."haha" is only (ha){2} → false.
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
Pin both ends.^ and $ reject anything before or after.
Short and long fail. Four or six digits both miss the exact count.
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.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
{n}Excellent
Bottom line: Safe everywhere. Use {n} whenever a token must repeat a fixed number of times.
Analysis
Performance Notes
Pattern style
Cost
Best for
\d{n} / \w{n}
Cheap
Fixed-width fields
^…{n}$
Cheap + early fail
Whole-string validation
Huge n on wildcards
Can grow
Prefer 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.
Remember
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.