Place {n,m} after a token to match it between n and m times inclusive. You get both a floor and a ceiling — unlike {n,}, which has no maximum.
Remember
token{n,m} → between n and m of token (inclusive)
\d{2,4} → "12" ✓ · "1234" ✓ · "1" ✗ · "12345" (finds "1234")
^.{8,64}$ → whole string length 8–64
{2} exact · {2,} two or more · {2,4} two to four
{0,1} = ? · {n,n} = {n}
Attach {n,m} when both a minimum and a maximum count matter.
Choose the token — e.g. \d, ., (ha), or a character class.
Add {n,m} with inclusive bounds — e.g. \d{2,4}, .{8,64}.
Expect greedy behavior: the engine prefers the longest run that still fits in the range.
Use ^ and $ when validating a whole field’s length.
Piece
Required?
Role
Preceding token
Yes
What repeats
{n,m}
Yes
Inclusive min / max count
^ / $
Optional
Needed for whole-string length checks
Pseudocode
Pseudocode
pattern = <token>{n,m}
// greedy: try up to m, then backtrack toward n if needed
count = 0
while count < m and next input matches <token>:
count = count + 1
if count < n:
fail
else:
// success — count is between n and m
continue with rest of pattern
Reference
Cheat Sheet
Keep these facts handy while writing bounded range patterns.
Topic
Detail
Meaning
Between n and m inclusive
Digits
/^\d{2,4}$/
Length band
/^.{8,64}$/
Group
(ha){2,3}
Shorthand
{0,1} = ? · {n,n} = {n}
Lazy
{n,m}? — prefer fewer within the range
Standard
ES3+ (universal)
Try it
Live Preview
Pick a range pattern and a sample string to see test() / match() results.
Browser demo of the {n,m} range quantifier — same idea as the examples below.
Result
pattern = /^\d{2,4}$/
text = "12"
test() → true
match() → ["12"]
/* whole string is 2–4 digits */
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Example 1 — Validate Two to Four Digits with ^\d{2,4}$
Accept whole values that contain only digits and fall within the range.
No anchors. The engine searches for a substring that fits the range.
Greedy max. From "12345" it takes four digits — the top of {2,4}.
Minimum still applies. A single "1" cannot satisfy {2,4}.
Global continues. With g, matching resumes after each run — here "aaa" then leftover "a".
Example 3 — Length Between 8 and 64 with ^.{8,64}$
Enforce both a minimum and a maximum length for an entire field.
JavaScript
const rule = /^.{8,64}$/;
console.log(rule.test("short")); // false — too short
console.log(rule.test("longenough")); // true
console.log(rule.test("a".repeat(64))); // true — at max
console.log(rule.test("a".repeat(65))); // false — over max
Any character.. matches one char; {8,64} bounds the total count.
Floor and ceiling. Shorter than 8 or longer than 64 both fail.
Whole field. Anchors measure the entire string, not a substring.
Size only. Add separate rules if you also need digits, symbols, or case.
Edge Cases & Pitfalls
Check these before relying on {n,m} in real projects.
greedy
Prefers the longest run in range
\d{2,4} on "12345" returns "1234", not "12". Use {n,m}? for the shortest valid run.
anchors
Without ^$, longer strings can still match
A five-digit string can contain a four-digit run. Anchor when the whole value must stay inside the range.
order
n must not be greater than m
Write {2,4}, never {4,2}. Equal bounds {3,3} mean the same as {3}.
choice
Need no upper bound?
Use {n,}. For zero-or-one, ? is the same as {0,1}.
Compatibility
Browser & Runtime Support
The {n,m} range quantifier is part of core JavaScript regex (ES3+). It works everywhere without polyfills.
✓ Baseline · ES3+
RegExp {n,m} quantifier
Supported in every modern and legacy browser, plus all Node.js versions. Universal syntax for bounded 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,m}Excellent
Bottom line: Safe everywhere. Use {n,m} whenever you need both a minimum and a maximum count.
Analysis
Performance Notes
Pattern style
Cost
Best for
\d{n,m} / \w{n,m}
Cheap
Bounded runs of known classes
^.{n,m}$
Cheap
Whole-string length bands
Wide ranges on . + nesting
Can grow
Prefer specific classes when possible
Bounded ranges are often safer than open-ended {n,} because the engine stops at m. Keep m realistic for your data, and prefer digit or word classes over bare . when you can.
Remember
Key Takeaways
Role:{n,m} means between n and m inclusive.
Family:{n} exact · {n,} no max · {n,m} bounded.
Greedy: longest allowed run wins unless you use {n,m}?.
Validation: pair with ^ and $ for whole-field length bands.
One line:{n,m} gives you a floor and a ceiling — the right tool when both bounds matter.
Frequently Asked Questions
The {n,m} quantifier means between n and m repetitions of the preceding token, inclusive. Example: \d{2,4} matches two, three, or four digits in a row.
{n} requires exactly n. {n,} requires at least n with no maximum. {n,m} sets both a minimum n and a maximum m—a bounded range.
Yes. The ? quantifier means zero or one, which is equivalent to {0,1}. Both are greedy by default; use ?? or {0,1}? for lazy matching.
{n,n} is the same as {n}—exactly n repetitions. For example, {2,2} matches only aa, just like {2}.
Yes. (ab){2,4} matches ab repeated two, three, or four times. Parentheses make the whole group the repeated token.
Bounded range quantifiers are core JavaScript regex syntax since ES3. They work in every browser and Node.js version without polyfills.