\d matches any single ASCII digit from 0 through 9. It is shorthand for [0-9]. The inverse is \D (any non-digit).
Remember
\d → one digit 0–9 (same as [0-9])
\d+ → one or more digits
\d{4} → exactly four digits
\D → one non-digit (same as [^0-9])
/\d/.test("room 5") → true
/\d/.test("hello") → false
"a12b".match(/\d+/g) → ["12"]
Match one digit, then add a quantifier when you need a full number or fixed length.
Write /\d/ to test for any digit in a string.
Use \d+ (or \d{n}) when you need more than one digit.
Add ^ and $ when the whole string must be digits only.
Use \D when you want the opposite: anything that is not a digit.
Piece
Required?
Role
\d in the pattern
Yes
Match one digit 0–9
Quantifier (+, {n})
Optional
Match runs or fixed counts
g flag
Optional
Find every digit / number run
Anchors ^$
For full-string checks
Validate the entire value
Pseudocode
Pseudocode
pattern has "\d"
read next character ch from the string
if ch is '0'..'9':
accept match
else:
fail this path
common patterns:
/\d/ → any digit present?
/\d+/g → every number token
/^\d{4}$/ → exactly four digits
/\D/ → any non-digit
Reference
Cheat Sheet
Keep these facts handy when matching digits.
Topic
Detail
Matches
One ASCII digit 0–9
Equivalent
[0-9]
Inverse
\D / [^0-9]
One or more
\d+
Exact count
\d{4}, \d{2,4}
Unicode digits
Not matched by default \d
Standard
ES3+ (universal)
Try it
Live Preview
Run common \d operations on sample text.
Browser demo of the RegExp \d digit escape — same ideas as the examples below.
Result
text = "room 5"
/\d/.test(text)
→ true
/* digit 0–9 is present */
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Example 1 — Detect Any Digit
Return true when the text contains at least one digit.
Validate fully: anchor with ^…$ when the whole string must be digits.
One line:\d finds digits — use \d+ to extract numbers and /^\d{n}$/ to validate fixed-length codes.
Frequently Asked Questions
\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. In standard JavaScript regex, \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 need the u flag and \p{Nd} on modern engines.
The \d digit shorthand is core regex syntax since ES3. It works in every browser and Node.js version.