DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk2 min

JavaScript Regex for a One- or Two-Digit Number

The JavaScript regex /^d{1,2}$/ matches a complete string of one or two digits, such as 3, 7, 12, or 45. Here is what each part means and how to adapt it.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use /^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.
  • d matches 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

/^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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
oneOrTwoDigits.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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.