JavaScript RegExp {n,m} Range Quantifier

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

What Is the {n,m} Quantifier?

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}
UsePrefer
Between n and mtoken{n,m}
Exactly ntoken{n}
At least n (no max)token{n,}

How to Use {n,m}

Attach {n,m} when both a minimum and a maximum count matter.

  1. Choose the token — e.g. \d, ., (ha), or a character class.
  2. Add {n,m} with inclusive bounds — e.g. \d{2,4}, .{8,64}.
  3. Expect greedy behavior: the engine prefers the longest run that still fits in the range.
  4. Use ^ and $ when validating a whole field’s length.
PieceRequired?Role
Preceding tokenYesWhat repeats
{n,m}YesInclusive min / max count
^ / $OptionalNeeded 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

Cheat Sheet

Keep these facts handy while writing bounded range patterns.

TopicDetail
MeaningBetween 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
StandardES3+ (universal)

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 */

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.

JavaScript
const rule = /^\d{2,4}$/;

["12", "123", "1234", "12345", "1"].forEach((s) => {
  console.log(s, rule.test(s));
});
Try It Yourself

How It Works

  1. Inclusive bounds. {2,4} allows 2, 3, or 4 digits.
  2. Whole string. ^ and $ reject extra characters before or after.
  3. In range passes. "12", "123", and "1234" all succeed.
  4. Outside fails. One digit is too short; five digits is too long.

Example 2 — Greedy Match Within the Range

See how \d{2,4} picks the longest allowed digit run inside a string.

JavaScript
console.log("12345".match(/\d{2,4}/));  // ["1234"]
console.log("99".match(/\d{2,4}/));      // ["99"]
console.log("1".match(/\d{2,4}/));       // null

console.log("aaaa".match(/a{1,3}/g));    // ["aaa", "a"]
Try It Yourself

How It Works

  1. No anchors. The engine searches for a substring that fits the range.
  2. Greedy max. From "12345" it takes four digits — the top of {2,4}.
  3. Minimum still applies. A single "1" cannot satisfy {2,4}.
  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
Try It Yourself

How It Works

  1. Any character. . matches one char; {8,64} bounds the total count.
  2. Floor and ceiling. Shorter than 8 or longer than 64 both fail.
  3. Whole field. Anchors measure the entire string, not a substring.
  4. 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}.

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 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,m} Excellent

Bottom line: Safe everywhere. Use {n,m} whenever you need both a minimum and a maximum count.

Performance Notes

Pattern styleCostBest for
\d{n,m} / \w{n,m}CheapBounded runs of known classes
^.{n,m}$CheapWhole-string length bands
Wide ranges on . + nestingCan growPrefer 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.

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.

Back to RegExp overview

Browse anchors, modifiers, metacharacters, and more regex topics.

RegExp hub →

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