Home JavaScript Regular expressions \D Definition
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.
\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" Use Prefer Detect a non-digit /\D/Keep digits only str.replace(/\D/g, "")Match digits \dSame as class [^0-9]
Approach
How to Use \D Use \D when you care about “not a digit” — detect it, extract it, or strip it away.
Write /\D/ to match one non-digit character. Add a quantifier (+, *) when you need a run of non-digits. Use the g flag with replace or match to process every match. Prefer \d when you want digits; prefer \D when you want everything else. Piece Required? Role \D in the patternYes Match one non-digit character Quantifier Optional Repeat (\D+, \D*, \D{2}) g flagOptional Find or replace every non-digit Anchors ^ $ Optional Require the whole string to be non-digits
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 Reference
Cheat Sheet Keep these facts handy while filtering digits and text.
Topic Detail Meaning Any character except ASCII 0–9 Equivalent [^0-9] (default / without Unicode digit extras)Opposite \d = digitOne or more \D+Strip formatting str.replace(/\D/g, "")Text-only check /^\D+$/Standard ES3+ (universal)
Try it
Live Preview Run common \D operations on sample strings.
Code
JavaScript Examples Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
1. Detect 2. Strip 3. Extract Example 1 — Detect Any Non-Digit Return true when at least one letter, space, or symbol appears.
console.log(/\D/.test("123")); // false
console.log(/\D/.test("a1")); // true
console.log(/\D/.test("Order #42")); // true How It Works Digits only. "123" has no non-digit, so /\D/ is false.Letter present. In "a1", "a" matches \D → true.Symbols count. Spaces and # in "Order #42" are non-digits too.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.
const order = "Order #42";
console.log(order.replace(/\D/g, "")); // "42"
const phone = "(555) 123-4567";
console.log(phone.replace(/\D/g, "")); // "5551234567" How It Works Global replace. /\D/g finds every non-digit in the string.Delete matches. Replacing with "" removes letters, spaces, and symbols.Digits remain. "Order #42" becomes "42"; the phone becomes digits only.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.
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"] ["abc", "def"]
["Price $", ".", " today"] How It Works Quantify. \D+ matches one or more consecutive non-digits.Digits are boundaries. In "abc123def456", digit runs split the text into "abc" and "def".Symbols included. Spaces, $, and . count as non-digits, so the price string yields three parts.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 escapingWrite 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.
Compatibility
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.
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.
Analysis
Performance Notes Approach Cost Best 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.
Remember
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 What does \D match in a JavaScript regular expression? \D matches any single character that is NOT an ASCII digit (0 through 9). Letters, spaces, punctuation, and symbols all satisfy \D.
Is \D the same as [^0-9] in JavaScript? Yes. For standard JavaScript regex without the u (Unicode) flag, \D and [^0-9] are equivalent—they both match one non-digit character.
What is the difference between \d and \D? \d matches digits 0–9. \D is the uppercase inverse and matches everything except those digits.
Does \D match multiple characters at once? 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.
How do I remove all non-digits and keep only numbers? 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.
Which browsers support \D in RegExp? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion