JavaScript RegExp \D Metacharacter

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Non-digit

What You’ll Learn

The \D metacharacter matches any single character that is not a digit from 0 through 9. It is the inverse of \d, equivalent to [^0-9], and essential when you need to isolate letters, strip formatting from phone numbers, or validate text-only input.

01

Non-digit

\D

02

Shorthand

[^0-9]

03

Inverse

Opposite of \d

04

Quantifiers

\D+

05

Strip

replace(/\D/g, "")

06

Validate

/^\D+$/

Introduction

Real-world strings rarely contain digits alone. Product codes, usernames, and formatted phone numbers mix letters, symbols, and numbers. The \D metacharacter lets you target everything that is not a numeral.

One \D matches exactly one non-digit character. To match a full word or symbol run, combine \D with quantifiers like + (one or more) or use the global flag with replace() to remove every non-digit at once.

Understanding the \D Metacharacter

\D is a negated predefined character class escape. In JavaScript it matches any character except ASCII digits 0 through 9.

JavaScript
/\D/.test("123");     // false (all digits)
/\D/.test("a1");      // true  ("a" is non-digit)
/\D/.test("hello");   // true  (letters)

"abc123def".match(/\D+/g);  // ["abc", "def"]
"(555) 123".replace(/\D/g, "");  // "555123"

The lowercase form \d matches digits only—equivalent to [0-9]. Think of \D as “not digit.”

💡
Beginner Tip

To normalize a phone number, write phone.replace(/\D/g, ""). The global flag removes every parenthesis, dash, and space, leaving digits only.

How to Use \D in JavaScript

Use \D in detection, extraction, cleaning, and validation workflows:

JavaScript
/\D/.test("4829");                   // false (digits only)
"abc123def456".match(/\D+/g);        // ["abc", "def"]
/^\D+$/.test("hello");               // true (no digits at all)
"Order #42".replace(/\D/g, "");      // "42"

Pair \D with anchors (^, $), quantifiers (+, *), and the global flag for bulk removal or extraction.

📝 Syntax

JavaScript
\D              // one non-digit character
\D+             // one or more non-digits
\D*             // zero or more non-digits
/\D/g           // global: every non-digit
\d              // one digit (inverse of \D)

Common patterns

  • /\D+/ — match a run of non-digit characters (words, symbols).
  • /^\D+$/ — validate that the string contains no digits.
  • /\D/g with replace("", ...) — strip all non-digits.
  • /\d|\D/ — match either a digit or a non-digit (any character).

⚡ Quick Reference

GoalPattern
One non-digit\D
Non-digit sequence\D+
No digits in string/^\D+$/
Remove non-digits/\D/g + replace("", ...)
Same as \D[^0-9]
Opposite\d

📋 \D vs [^0-9] vs \d

Three related patterns for non-digits and digits.

Non-digit
\D

Shorthand

Class
[^0-9]

Equivalent

Digit
\d

Inverse

Many non-digits
\D+

Quantifier

Examples Gallery

Open DevTools Console (F12) or use Try-it links. Examples cover detection, extraction, digit stripping, validation, and parsing mixed IDs.

📚 Getting Started

Detect whether a string contains any non-digit character.

Example 1 — Test for Any Non-Digit

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

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

A single \D matches one non-digit anywhere in the string. Pure digit strings like "123" never satisfy the pattern.

📈 Practical Patterns

Extract, clean, validate, and parse with non-digit patterns.

Example 2 — Extract Non-Digit Sequences

Pull letter and symbol runs from mixed alphanumeric text.

JavaScript
const mixed = "abc123def456";

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

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

How It Works

\D+ greedily collects consecutive non-digits. Digits act as boundaries, splitting the string into separate matches.

Example 3 — Strip Non-Digits to Keep Numbers Only

Remove formatting characters from phone numbers and order IDs.

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

The global flag with /\D/g replaces every non-digit with an empty string. Digits remain untouched—one of the most common \D patterns in production code.

Example 4 — Validate Text With No Digits

Ensure input contains only non-digit characters (letters and symbols).

JavaScript
const noDigits = /^\D+$/;

console.log(noDigits.test("hello"));    // true
console.log(noDigits.test("user_name")); // true
console.log(noDigits.test("abc123"));   // false
console.log(noDigits.test("4829"));     // false
Try It Yourself

How It Works

^ and $ anchor the match to the full string. \D+ requires one or more non-digit characters with no numerals allowed.

Example 5 — Split a Mixed Product ID

Separate the letter prefix from the numeric suffix in a SKU-style code.

JavaScript
const sku = "SKU-A42B";

const parts = sku.split(/\d+/);
console.log(parts); // ["SKU-A", "B"]

const prefix = sku.match(/^(\D+)\d/);
console.log(prefix[1]); // "SKU-A"
Try It Yourself

How It Works

Splitting on /\d+/ breaks the string at digit runs. Capturing with (\D+) extracts the text portion before the first digit.

🚀 Common Use Cases

  • Phone normalization — strip dashes and parentheses with replace(/\D/g, "").
  • Letter extraction — pull word tokens from mixed strings using \D+.
  • Username rules — reject inputs that contain digits with /\D/ or /^\D+$/.
  • Data cleaning — remove currency symbols and commas before parsing numbers.
  • SKU parsing — split product codes at digit boundaries.
  • Filtering — detect formatted vs plain numeric strings.

Important Considerations

  • One vs many — lone \D matches one non-digit; use + for sequences.
  • Global flag — omitting g in replace() removes only the first non-digit.
  • ASCII only — default \D treats ASCII 0–9 as digits; Unicode numerals need the u flag and \P{Nd}.
  • Whitespace counts — spaces, tabs, and newlines are non-digits and match \D.
  • Same as [^0-9] — pick one style and stay consistent within a codebase.

🧠 How \D Matches Non-Digits

1

Read character

The engine inspects the next character at the current position.

Scan
2

Check not 0–9

\D succeeds only if the character is NOT an ASCII digit.

Match
3

Apply quantifier

With + or global replace, the engine repeats the non-digit check.

Extend
4

Continue pattern

Non-digit matched → advance. Digit found → fail or backtrack.

📝 Notes

  • \D equals [^0-9] in standard JavaScript regex.
  • \d equals [0-9] (the inverse pair).
  • Review \d digit metacharacter and [^0-9] negated class.
  • Next up: \S metacharacter for non-whitespace.
  • For letters only (excluding digits and symbols), use /^[a-zA-Z]+$/ instead of /^\D+$/.
  • Combine with \b for word tokens: \b\D+\b matches non-digit words.

Browser & Runtime Support

The \D non-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 non-digits.

99% Universal syntax
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 everywhere. Use \D as the standard shorthand for matching non-digits.

Conclusion

The \D metacharacter is the fastest way to match non-digits in JavaScript regular expressions. It covers letters, spaces, punctuation, and symbols—everything except 0 through 9.

Use \D+ to extract text runs, /\D/g with replace() to normalize formatted numbers, and remember that it is the uppercase inverse of \d. Next, learn \S for non-whitespace matching.

💡 Best Practices

✅ Do

  • Use /\D/g to strip formatting from phone numbers
  • Use \D+ to extract letter sequences from mixed text
  • Anchor with ^ and $ for digit-free validation
  • Know that \D and [^0-9] are equivalent
  • Pair with \d when parsing structured codes

❌ Don’t

  • Expect \D alone to match multi-character words
  • Forget the global flag when removing all non-digits
  • Assume /^\D+$/ means “letters only”
  • Assume Unicode numerals are excluded without the u flag
  • Mix \D and manual [^0-9] randomly in one project

Key Takeaways

Knowledge Unlocked

Five things to remember about RegExp \D

Match anything except digits 0 through 9.

5
Core concepts
📝 02

= [^0-9]

Same set.

Compare
📈 03

\D+

Sequences.

Quantifier
🔒 04

/\D/g

Strip chars.

Replace
🔢 05

\d

Inverse.

Pair

❓ 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. Together they partition the ASCII character set for basic parsing.
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.
Did you know?

JavaScript provides uppercase inverses for three common shorthands: \D (not digit), \S (not whitespace), and \W (not word character). Uppercase always means “everything except” the lowercase form.

Continue to \S non-whitespace metacharacter

Learn how uppercase S 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.

6 people found this page helpful