Pattern library / Numbers & Codes
Regex to Match a Number in a Range (1 to 100)
Validating that a number falls inside a range is one of the most-asked regex questions (regex number between 1 and 100, scores 1-5, CIDR bits 1-32). Regex cannot compare values numerically, so a range is expressed as alternation over digit shapes: the short values, the mid values, and the exact upper bound — anchored, with no leading zeros and no decimals unless you want them.
How the pattern works
- The pattern is
^-anchored and$-anchored, so it validates the whole string — a search-mode match would happily find7inside101. [1-9][0-9]?covers 1-99 in two digit shapes; the literal100handles the bound that has no uniform shape. That is the general recipe:^(UB|[1-9][0-9]?)$for 1-UB.- No leading zeros:
007,042are rejected on purpose — form input and IDs usually should not accept them. Relax with^[0-9]{1,3}$if you do not care. - For a 0-100 range add
0|in front:^(100|[1-9]?[0-9]|0)$. For 1-1000 it becomes^(1000|9[0-9]{2}|[1-9][0-9]{0,2})$— same shape, one more tier.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
7 | Match | single digit in range | Try → |
42 | Match | two digits, no leading zero | Try → |
100 | Match | exact upper bound via alternation | Try → |
0 | No match | range starts at 1 | Try → |
007 | No match | leading zeros rejected | Try → |
101 | No match | above the upper bound | Try → |
7.5 | No match | integers only — decimals fail the $ anchor | Try → |
Where to use it
JavaScript (form validation)
const inRange = /^(100|[1-9][0-9]?)$/;
inRange.test('42'); // true
inRange.test('007'); // false
inRange.test('101'); // false
HTML input pattern attribute
<input type="text" inputmode="numeric"
pattern="(100|[1-9][0-9]?)"
title="Enter a number from 1 to 100">
SQL (BigQuery, RE2)
SELECT score FROM results WHERE REGEXP_CONTAINS(CAST(score AS STRING), r'^(100|[1-9][0-9]?)$')
Common mistakes
[1-32]is a character class, not a range: it matches any single character from1to3, plus2and3— so it accepts29. This is the classic CIDR-port mistake; build the range from digit shapes instead.- Dropping the anchors turns validation into search:
100|[1-9][0-9]?without^/$matches101(it finds10or01-shaped substrings). - Big ranges get unreadable fast — past ~4 tiers use a numeric comparison (
n >= 1 && n <= 100) instead of regex; regex validates shape, not magnitude.
FAQ
How do I match a number between 1 and 100 in regex?
Use ^(100|[1-9][0-9]?)$. It accepts 1-99 (no leading zeros) and the literal 100, and rejects 0, 007, 101 and 7.5.
Why can't I just write [1-100]?
Square brackets are a character class: they match exactly one character drawn from the set inside. [1-100] means 'one character that is 1, 0 or 0' — nothing to do with the number 100.
How do I allow decimals like 7.5?
Extend the pattern with an optional decimal part, e.g. ^(100|[1-9][0-9]?)(\.[0-9]{1,2})?$ for 0.00-100.99 style input, or handle the fraction in your range check instead.
Related patterns
Percentage Regex — Match Percent Values — Test & Copy · Extract Numbers from String — Regex + Examples · IPv4 CIDR Regex — Match IP/Prefix — Test & Copy
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.