\D: everything that is not a digit
\D matches any single character that is not one of the ASCII digits 0 to 9. It is the exact complement of \d, so it includes spaces, punctuation, newlines and every letter.
At a glance
- Syntax
- \D
- Equivalent to
- [^0-9]
- Includes
- letters, punctuation, whitespace and line terminators
- Positive form
- \d
- Common use
- replace(/\D/g, "") to reduce a value to its digits before validating it
- Any character idiom
- [\d\D] matches literally anything, including newlines, with no s flag
In detail
The most common use of \D has nothing to do with matching and everything to do with normalising. Stripping every non-digit from a phone number, a card number or a reference code turns a value the user typed however they liked into a fixed length string you can then check. Doing that first is nearly always simpler than writing one pattern that tolerates every possible separator.
It is also the quickest way to split on unknown separators. Splitting an ISO date on /\D/ returns the three numeric parts without you having to state whether the separator was a hyphen, a slash or a full stop. That is a virtue when you are parsing something loose and a liability when you are validating something strict, because the split will happily succeed on nonsense too.
Because \D is a true complement it includes characters people forget about: spaces, tabs, newlines and every letter in every script. A pattern such as /^\D+$/ accepts a string of nothing but spaces, so it is not a test for a word or a name however much it reads like one.
Pairing the two shorthands gives the standard any character idiom. [\d\D] is a class that contains a digit or a non-digit, which is every character there is, including line terminators. It does the same job as [\s\S] and does not need the s flag, which matters when the pattern has to run somewhere dotAll is not available.
Examples
"a1b2".replace(/\D/g, ""); // "12"
"2026-03-08".split(/\D/); // ["2026", "03", "08"]/^\D+$/.test(" "); // true, spaces are not digits
/^[\d\D]+$/.test("a\nb"); // true, this class covers newlines tooThings that catch people out
- /^\D+$/ is not a check for letters. A string of spaces, punctuation or newlines passes it, because none of those are digits.
- Splitting on /\D/ rather than /\D+/ produces empty strings between consecutive separators, so "1--2".split(/\D/) has three elements rather than two.
- Stripping non-digits before validating discards the sign, so a value such as "-40" becomes "40" and a negative number silently passes a positive-only check.
Try it: Regex Tester
Type a pattern and a test string, watch every match highlight as you go, read a plain English breakdown of each token, and check the numbered and named groups it captured.
Regex Tester
LOCAL · Browser-OnlyTest and debug regular expressions with live matching and highlights
Related Regex syntax
All regex syntax →\d: the digit class, and the digits it refuses
In JavaScript \d is exactly [0-9], even with the u flag. Here is why other engines disagree and what to use when you need Unicode digits.
[^abc]: negated character sets that quietly cross newlines
A negated class matches one character that is not in the set, including line terminators. That is why a quoted string pattern runs across lines.
Regex for numbers: integers, decimals and negative signs
The integer preset splits 3.14 into two matches and the float preset ignores whole numbers. Here is the combined pattern and what it still leaves out.
\w: the word character class, underscore included
\w is exactly [A-Za-z0-9_] in JavaScript. That includes the underscore and excludes every accented letter, which is where most \w bugs come from.
\W: non word characters, and the underscore it will not match
\W is the complement of \w, so it never matches an underscore and always matches an accented letter. Both surprises show up when splitting identifiers.
\s: every whitespace character it actually matches
The full list behind \s in JavaScript, including the no break space and the byte order mark, plus the one invisible character it deliberately skips.
Tools for this
- Regex Tester Free online regex tester - test and debug regular expressions with live matching and highlights
- Regex Playground Regex playground with live match highlighting, named groups, find and replace, and shareable links