JavaScript RegExp Digit Metacharacter (\d)

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is \d?

\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"]
UsePrefer
Detect a digit/\d/
Extract numbers/\d+/g
Fixed-length code/^\d{4}$/
Non-digit character/\D/

How to Use \d

Match one digit, then add a quantifier when you need a full number or fixed length.

  1. Write /\d/ to test for any digit in a string.
  2. Use \d+ (or \d{n}) when you need more than one digit.
  3. Add ^ and $ when the whole string must be digits only.
  4. Use \D when you want the opposite: anything that is not a digit.
PieceRequired?Role
\d in the patternYesMatch one digit 0–9
Quantifier (+, {n})OptionalMatch runs or fixed counts
g flagOptionalFind every digit / number run
Anchors ^ $For full-string checksValidate 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

Cheat Sheet

Keep these facts handy when matching digits.

TopicDetail
MatchesOne ASCII digit 0–9
Equivalent[0-9]
Inverse\D / [^0-9]
One or more\d+
Exact count\d{4}, \d{2,4}
Unicode digitsNot matched by default \d
StandardES3+ (universal)

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 */

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.

JavaScript
console.log(/\d/.test("hello"));     // false
console.log(/\d/.test("room 5"));    // true
console.log(/\d/.test("Order #42")); // true
Try It Yourself

How It Works

  1. Letters only. "hello" has no 0–9 → false.
  2. One digit is enough. "room 5" contains 5 → true.
  3. Anywhere in the string. "Order #42" matches on 4 (or 2).
  4. One character. Lone \d does not require the whole string to be numeric.

Example 2 — Extract Number Sequences

Collect every run of consecutive digits with \d+ and the g flag.

JavaScript
const text = "SKU-12 costs $19.99 today";

const numbers = text.match(/\d+/g);
console.log(numbers); // ["12", "19", "99"]

const first = text.match(/\d+/);
console.log(first[0]); // "12"
Try It Yourself

How It Works

  1. + means one or more. Digits stick together into tokens like "12".
  2. Global finds every run. g returns all matches, not only the first.
  3. Dots split numbers. 19.99 becomes "19" and "99" because . is not a digit.
  4. First match only. Without g, match(/\d+/) gives the first number token.

Example 3 — Validate a Four-Digit PIN

Require the entire string to be exactly four digits.

JavaScript
const pinPattern = /^\d{4}$/;

console.log(pinPattern.test("4821"));  // true
console.log(pinPattern.test("482"));   // false
console.log(pinPattern.test("4821a")); // false
console.log(pinPattern.test("48 21")); // false
Try It Yourself

How It Works

  1. ^ and $. The match must cover the whole string.
  2. \d{4}. Exactly four digit characters in a row.
  3. Wrong length fails. Three digits or five characters (with a letter) are rejected.
  4. Spaces count. "48 21" is not four digits — the space breaks the pattern.

Edge Cases & Pitfalls

Check these before relying on \d in real projects.

one

Lone \d is one digit

Use \d+ or \d{n} for multi-digit numbers. See also + and {n}.

decimal

Decimals stop at the dot

"19.99".match(/\d+/g) is ["19", "99"]. For simple floats use a pattern that includes \..

ascii

ASCII digits only by default

Default \d does not match Unicode numerals. For most forms and IDs, ASCII 0–9 is what you want.

escape

new RegExp needs double escaping

Write new RegExp("\\d+"). In a string pattern argument you need two backslashes.

Browser & Runtime Support

The \d digit metacharacter is core regex syntax (ES3+) and works everywhere JavaScript runs.

✓ Baseline · ES3+

RegExp \d

Supported in every modern and legacy browser, plus all Node.js versions. \d and [0-9] behave identically for ASCII digits.

100% Universal support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge Supported · Chromium & Legacy
Full support
Internet Explorer Supported · Legacy
Full support
Opera All modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
\d Excellent

Bottom line: Safe everywhere. Use \d as the standard shorthand for matching digits 0–9.

Performance Notes

ApproachCostBest for
/\d/.test(str)O(n)Quick digit presence checks
str.match(/\d+/g)O(n)Extract all number tokens
/^\d{n}$/.test(str)O(n)Fixed-length validation

Matching digits is cheap. Prefer a clear pattern (\d+, ^\d{4}$) over complicated classes — engines optimize the digit shorthand well.

Key Takeaways

  • Role: \d matches one ASCII digit 0–9 (same as [0-9]).
  • Quantify: use \d+ or \d{n} for longer numbers and codes.
  • Inverse: \D matches any non-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.

Next: . (dot)

Learn how the dot metacharacter matches almost any character.

. tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful