Module 2 · Lesson 2 of 46/32 in the course~16 min
Module lessons (2/4)
Exact quantities `{n,m}`
Lesson preview5 hands-on exercises readyRegex Course · Module 2 — Quantifiers
Exactly n, from n to m, at least n: the numeric quantifier.
What you'll learn in this lesson
- Specify exact numeric repetitions with the syntax
{n}(\d{4}for 4-digit years). - Define closed intervals
{n,m}and lower open limits{n,}by understanding what happens to longer strings in the absence of anchors. - Avoid two frequent syntax errors:
{2, 4}with space after the decimal point and the omitted leading zero{,4}(which in JS requires{0,4}). - Recognize how the engine tries the maximum limit first (
m) and then backs off if the rest of the pattern fails.
Overview
When * and + are too general and you need precise numerical control over the number of repetitions, use the quantifiers in curly brackets: {n}, {n,m} and {n,}. Like all basic quantifiers, they are greedy: they first look for the largest number allowed and reduce only if forced.
Sections covered in this lesson
- 1Mental model: valid shapes and invalid shapes in JavaScript
- 2Progressive examples
- 3Guided laboratory
- 4Check for understanding
Pro Lesson · Module 2 — Quantifiers
Unlock "Exact quantities `{n,m}`" and the full Regex Course curriculum
Exactly `n`, from `n` to `m`, at least `n`: the numeric quantifier.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Regex Course plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.