Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsUse /^d{1,2}$/ when a JavaScript string must contain exactly one or two digits. It matches 3, 7, 12, and 45, while rejecting an empty string, three-digit text, letters, spaces, and decimal points.
The pattern
/^d{1,2}$/
^anchors the match at the start of the string.dmatches a digit in this JavaScript pattern.{1,2}requires at least one and at most two repetitions.$anchors the match at the end of the string.
Because both anchors are present, the entire value must be one or two digits rather than merely containing one or two digits somewhere inside longer text.
Why the comma matters
The comma in {1,2} separates the minimum and maximum repetition counts. Writing {1|2} does not mean “one or two repetitions”; the pipe is alternation syntax, not the separator used by a range quantifier.
JavaScript example
const oneOrTwoDigits = /^d{1,2}$/;
oneOrTwoDigits.test("3"); // true
oneOrTwoDigits.test("45"); // true
oneOrTwoDigits.test("123"); // false
oneOrTwoDigits.test(12); // false: this test expects a string
The SitePoint discussion addressed numeric-looking strings in a larger expression, not numbers after JavaScript has parsed them. If your input may be a number, convert it deliberately before testing—for example, oneOrTwoDigits.test(String(value))—and decide separately whether values such as negative numbers, decimals, or leading zeros should be allowed.
#1 Best Overall
What it accepts and rejects
| Input | Result | Reason |
|---|---|---|
"3" |
Matches | One digit |
"12" |
Matches | Two digits |
"" |
Does not match | At least one digit is required |
"123" |
Does not match | More than two digits |
" 7" |
Does not match | Space is not a digit and the anchors cover the whole string |
"7.5" |
Does not match | The pattern allows digits only |
"-7" |
Does not match | No sign is included in the pattern |
If the allowed lengths are five or nine digits
A range such as {5,9} would also allow six, seven, and eight digits. For exactly five or exactly nine digits, use separate alternatives:
/^(?:d{5}|d{9})$/
The SitePoint thread also showed an equivalent optional-group form:
Rank #2
/^d{5}(d{4})?$/
That second form means five digits followed by an optional additional four digits, so the only permitted lengths are five and nine.
Common adjustments
Allow surrounding whitespace
If whitespace should be ignored, trim the value before testing rather than silently broadening the validation rule:
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11oneOrTwoDigits.test(input.trim())
Require a numeric range instead of a digit count
This regex checks textual shape only. It does not enforce a domain such as 1–31 or 0–99. For a numeric range, parse and validate the value separately, because a string like "09" has two digits but may or may not be acceptable for your application.
Answer
For a whole JavaScript string containing one or two digits, use /^d{1,2}$/. The 1,2 quantifier is the key: it sets the minimum and maximum repetition counts.
Quick Recap
Best Value
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




