JavaScript RegExp Non-Digit Metacharacter (\D)

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

What Is \D?

\D matches any single character that is not an ASCII digit 0–9. It is the opposite of \d and equals [^0-9] in ordinary JavaScript regex.

Remember
\d  → one digit (0–9)
\D  → one non-digit (everything else)
\D+ → one or more non-digits

/\D/.test("123")   → false
/\D/.test("a1")    → true
" (555) ".replace(/\D/g, "") → "555"
UsePrefer
Detect a non-digit/\D/
Keep digits onlystr.replace(/\D/g, "")
Match digits\d
Same as class[^0-9]

How to Use \D

Use \D when you care about “not a digit” — detect it, extract it, or strip it away.

  1. Write /\D/ to match one non-digit character.
  2. Add a quantifier (+, *) when you need a run of non-digits.
  3. Use the g flag with replace or match to process every match.
  4. Prefer \d when you want digits; prefer \D when you want everything else.
PieceRequired?Role
\D in the patternYesMatch one non-digit character
QuantifierOptionalRepeat (\D+, \D*, \D{2})
g flagOptionalFind or replace every non-digit
Anchors ^ $OptionalRequire the whole string to be non-digits

Pseudocode

Pseudocode
pattern has "\D"
read next character ch from the string

if ch is digit '0'..'9':
    fail this path
else:
    accept ch as a non-digit match

common actions:
  test(str)              → true if any non-digit exists
  match(/\D+/g)          → non-digit runs
  replace(/\D/g, "")     → digits only

Cheat Sheet

Keep these facts handy while filtering digits and text.

TopicDetail
MeaningAny character except ASCII 0–9
Equivalent[^0-9] (default / without Unicode digit extras)
Opposite\d = digit
One or more\D+
Strip formattingstr.replace(/\D/g, "")
Text-only check/^\D+$/
StandardES3+ (universal)

Live Preview

Run common \D operations on sample strings.

Browser demo of the RegExp \D non-digit escape — same ideas as the examples below.

Result
text = "123"
/\D/.test(text)
→ false

/* every character is a digit 0–9 */

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Detect Any Non-Digit

Return true when at least one letter, space, or symbol appears.

JavaScript
console.log(/\D/.test("123"));        // false
console.log(/\D/.test("a1"));         // true
console.log(/\D/.test("Order #42"));  // true
Try It Yourself

How It Works

  1. Digits only. "123" has no non-digit, so /\D/ is false.
  2. Letter present. In "a1", "a" matches \D → true.
  3. Symbols count. Spaces and # in "Order #42" are non-digits too.
  4. One char is enough. test stops at the first match.

Example 2 — Strip Non-Digits (Keep Numbers)

Remove formatting characters from order codes and phone numbers.

JavaScript
const order = "Order #42";
console.log(order.replace(/\D/g, "")); // "42"

const phone = "(555) 123-4567";
console.log(phone.replace(/\D/g, "")); // "5551234567"
Try It Yourself

How It Works

  1. Global replace. /\D/g finds every non-digit in the string.
  2. Delete matches. Replacing with "" removes letters, spaces, and symbols.
  3. Digits remain. "Order #42" becomes "42"; the phone becomes digits only.
  4. Common pattern. This is a standard way to normalize phone and ID input.

Example 3 — Extract Non-Digit Sequences

Pull letter and symbol runs from mixed alphanumeric text.

JavaScript
const mixed = "abc123def456";
console.log(mixed.match(/\D+/g)); // ["abc", "def"]

const price = "Price $19.99 today";
console.log(price.match(/\D+/g));
// ["Price $", ".", " today"]
Try It Yourself

How It Works

  1. Quantify. \D+ matches one or more consecutive non-digits.
  2. Digits are boundaries. In "abc123def456", digit runs split the text into "abc" and "def".
  3. Symbols included. Spaces, $, and . count as non-digits, so the price string yields three parts.
  4. Global match. The g flag returns every run, not only the first.

Edge Cases & Pitfalls

Check these before relying on \D in real projects.

ascii

Default \D is ASCII digits only

Without Unicode property escapes, “digit” means 0–9. Other numeral systems need \p{Nd} with the u flag on modern engines.

empty

\D needs a character

/\D/.test("") is false — there is nothing to match. Use /^\D*$/ if the empty string should pass.

escape

new RegExp needs double escaping

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

scope

\D is broader than “letters”

Spaces, punctuation, and emoji all match. For letters only, prefer [a-zA-Z] or \p{L} with u.

Browser & Runtime Support

The \D non-digit escape 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. No polyfill required.

100% Universal support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · 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 to use in any JavaScript environment. Remember that default \D targets ASCII digits 0–9.

Performance Notes

ApproachCostBest for
/\D/.test(str)O(n)Quick “has non-digit?” checks
str.replace(/\D/g, "")O(n)Digit-only cleanup
str.match(/\D+/g)O(n)Extracting non-digit runs

Character-class style matches are cheap. Prefer \d when you want digits directly, and \D when stripping or rejecting them is clearer.

Key Takeaways

  • Role: \D matches one character that is not a digit 0–9.
  • Opposite of \d: digits vs everything else.
  • Cleanup: replace(/\D/g, "") keeps digits only.
  • Equivalent: \D ≈ [^0-9] in everyday JS regex.

One line: \D means “not a digit” — detect it, extract runs with \D+, or strip it with replace(/\D/g, "").

Frequently Asked Questions

\D matches any single character that is NOT an ASCII digit (0 through 9). Letters, spaces, punctuation, and symbols all satisfy \D.
Yes. For standard JavaScript regex without the u (Unicode) flag, \D and [^0-9] are equivalent—they both match one non-digit character.
\d matches digits 0–9. \D is the uppercase inverse and matches everything except those digits.
By itself, \D matches exactly one non-digit. Add a quantifier like + for one or more (\D+) or * for zero or more (\D*) to match longer text runs.
Use replace with the global flag: str.replace(/\D/g, ""). Every non-digit is removed, leaving digits only—handy for phone numbers and formatted IDs.
The \D non-digit shorthand is core regex syntax since ES3. It works in every browser and Node.js version.

Next: \S non-whitespace

Learn how the \S metacharacter matches any character that is not whitespace.

\S 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.

8 people found this page helpful