Course contentsShow
← JavaScript
Lesson 1685 of 2,72336. Regular ExpressionsFree lesson

Exact and Range Quantifiers

Specify exact repetitions with {n}, minimums with {n,}, and ranges with {n,m}.

Exact and Range Quantifiers

You'll learn how to specify precise repetition counts in regex patterns using curly brace quantifiers.

Why Precision Matters

So far you've used +, *, and ? to match "one or more," "zero or more," or "optional." But what if you need exactly 5 digits, at least 3 letters, or between 2 and 4 vowels? That's where curly braces come in.

The Three Forms

Exact Count: {n}

Match exactly n repetitions:

let zipCode = /^\d{5}$/;
console.log(zipCode.test("12345")); // true
console.log(zipCode.test("1234"));  // false (too short)
console.log(zipCode.test("123456")); // false (too long)

Minimum Count: {n,}

Match at least n repetitions (no upper limit):

let strongPassword = /[A-Z]{3,}/;
console.log(strongPassword.test("AB"));   // false
console.log(strongPassword.test("ABC"));  // true
console.log(strongPassword.test("ABCDEF")); // true

Range: {n,m}

Match between n and m repetitions (inclusive):

let username = /^[a-z]{3,8}$/;
console.log(username.test("ab"));      // false (too short)
console.log(username.test("alice"));   // true
console.log(username.test("verylongname")); // false (too long)

Practical Example

Validating a phone number with optional area code:

let phone = /^\d{3}-\d{3}-\d{4}$/;
console.log(phone.test("555-123-4567")); // true
console.log(phone.test("55-123-4567"));  // false

Think of quantifiers like ordering: + is "as much as possible," but {3,5} is "between 3 and 5 slices, no more, no less."

Key Takeaway: Use {n} for exact counts, {n,} for minimums, and {n,m} for ranges—giving you surgical control over repetition in your regex patterns.