The \d metacharacter matches any single digit from 0 through 9. It is shorthand for [0-9] and appears in nearly every pattern that validates numbers, extracts digits, or parses formatted text like phone numbers and dates.
01
Digit
\d
02
Shorthand
[0-9]
03
One char
Single digit
04
Quantifiers
\d+
05
Opposite
\D
06
Validate
\d{4}
Fundamentals
Introduction
Many real-world strings mix letters and numbers: order IDs, ages, prices, and verification codes. The \d metacharacter gives you a compact way to target digits without listing every numeral.
One \d matches exactly one digit. To match a full number, combine \d with quantifiers like + (one or more) or {n} (exactly n digits).
Concept
Understanding the \d Metacharacter
\d is a predefined character class escape. In JavaScript it matches ASCII digits 0 through 9 only.
JavaScript
/\d/.test("abc"); // false (no digits)
/\d/.test("room 5"); // true ("5" matches)
"price: $19".match(/\d+/); // ["19"]
/\d{3}/.test("pin 482"); // true (482 is three digits)
The uppercase form \D matches any character that is not a digit—equivalent to [^0-9].
💡
Beginner Tip
When you need a four-digit PIN, write /^\d{4}$/. The anchors ensure the entire string is exactly four digits—no letters before or after.
Usage
How to Use \d in JavaScript
Use \d in detection, extraction, validation, and replacement workflows:
The \d digit metacharacter is part of core JavaScript regular expression syntax.
✓ Baseline · ES3+
RegExp \d
Supported in every browser and Node.js version. \d and [0-9] behave identically for ASCII digits.
99%Universal syntax
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
\dExcellent
Bottom line: Safe everywhere. Use \d as the standard shorthand for matching digits 0–9.
Wrap Up
Conclusion
The \d metacharacter is the fastest way to match digits in JavaScript regular expressions. It covers 0 through 9, pairs naturally with quantifiers and anchors, and has a clear inverse in \D.
Use \d+ to extract numbers, /^\d{n}$/ to validate fixed-length codes, and remember that it matches ASCII digits only by default. Next, learn the . (dot) metacharacter.
\d matches any single ASCII digit from 0 through 9. It is shorthand for the character class [0-9] and is one of the most common regex building blocks.
Yes, for standard JavaScript regex without the u (Unicode) flag, \d and [0-9] both match digits 0–9. Choose whichever is clearer in your pattern.
By itself, \d matches exactly one digit. Add a quantifier like + for one or more (\d+) or {4} for exactly four (\d{4}) to match longer number sequences.
\D is the opposite of \d—it matches any single character that is NOT a digit. It is equivalent to [^0-9] in JavaScript.
In default JavaScript regex, \d matches ASCII 0–9 only. Full Unicode digit categories require the u flag and \p{Nd} property escapes on modern engines.
The \d digit shorthand is core regex syntax since ES3. It works in every browser and Node.js version.
Did you know?
JavaScript also provides \w (word character: letters, digits, underscore) and \s (whitespace). Together with \d, these three shorthand classes cover most basic parsing tasks without writing full character classes.