The negated class [^0-9] matches any single character that is not a digit. It is the flip side of [0-9] and pairs perfectly with replace() when you need to strip formatting or isolate letters from numeric text.
01
Negate
[^0-9]
02
Not digit
Letters, symbols
03
Like \D
Same in JS
04
Strip
replace → ""
05
^ rules
Inside [ ] only
06
vs [0-9]
Opposite sets
Fundamentals
Introduction
Character classes define which characters may appear at a position. A negated class flips the rule: match anything except the listed characters. With [^0-9], you target letters, spaces, punctuation, and symbols while excluding digits.
This pattern appears constantly in real code—cleaning phone numbers, validating usernames without numbers, parsing mixed strings, and filtering user input before storage.
Concept
Understanding the [^0-9] Character Class
Place ^ immediately after the opening bracket to negate the class. [^0-9] means “one character that is not 0, 1, 2, …, or 9.”
JavaScript
/[^0-9]/.test("123"); // false (digits only)
/[^0-9]/.test("abc"); // true (letters are non-digits)
/[^0-9]/.test("a1"); // true ("a" is a non-digit)
In JavaScript, [^0-9] behaves the same as the shorthand \D (capital D). Both match a single non-digit character.
💡
Beginner Tip
Do not confuse ^ inside [ ] (negation) with ^ outside brackets (start-of-string anchor). In /^[0-9]/ the first ^ anchors; in /[^0-9]/ it negates.
Usage
How to Use [^0-9] in JavaScript
The most common pattern strips non-digits by replacing them with an empty string:
Use test() to detect non-digit content, match() to extract letter runs, and replace(/[^0-9]/g, "") to keep digits only.
Foundation
📝 Syntax
JavaScript
/[^0-9]/ // one non-digit
/[^0-9]+/ // one or more non-digits in a row
/\D/ // shorthand equivalent
str.replace(/[^0-9]/g, "") // remove all non-digits
Checking “no digits” is often clearer as !/[0-9]/.test(name). The anchored form /^[^0-9]+$/ additionally requires the entire string to be built from non-digits only.
Applications
🚀 Common Use Cases
Phone/card cleanup — replace(/[^0-9]/g, "") before validation or storage.
Username rules — reject handles that contain numbers.
Log parsing — split or extract text portions from mixed logs.
Input sanitization — detect unexpected symbols in numeric fields.
Masking — replace non-digits when normalizing formatted display values.
Building negated sets — same ^ trick for [^abc] and other exclusions.
Watch Out
Important Considerations
^ placement — only the first character after [ negates. [0^9] is not the same as [^0-9].
One character per token — use + to match runs of non-digits.
Whitespace counts — spaces, tabs, and newlines are non-digits and match [^0-9].
Unicode digits — JavaScript [0-9] and [^0-9] focus on ASCII digits; other numeral scripts may not behave as expected.
Double negative logic — “no digits” is often written as !/\d/.test(s) for readability.
🧠 How [^0-9] Matches Text
1
Negate the set
^ after [ inverts 0-9 so digits are excluded from the allowed set.
Invert
2
Scan character
The engine checks if the current character is outside the digit range.
Compare
3
Apply globally
With g, repeat for every position—essential for replace cleanup.
Repeat
4
🚫
Match or skip
Non-digit → match. Digit → skip and try next position.
Important
📝 Notes
\D is the shorthand for [^0-9] in JavaScript.
To remove digits instead, use replace(/[0-9]/g, "").
Combine with anchors: /^[^0-9]+$/ for strings with zero digits.
Review [0-9] for the positive digit class counterpart.
In character classes, most metacharacters lose special meaning except ^, -, ], and \.
Compatibility
Browser & Runtime Support
Negated character classes like [^0-9] are part of core JavaScript regular expression syntax.
✓ Baseline · ES3+
RegExp [^0-9]
Supported in every browser and Node.js version. The \D shorthand is equally universal.
99%Universal syntax
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
[^0-9]Excellent
Bottom line: Safe everywhere. Use [^0-9] or \D confidently for ASCII non-digit matching in all JavaScript runtimes.
Wrap Up
Conclusion
The [^0-9] negated class is the complement of [0-9]: it matches anything that is not a digit. Master it alongside replace() for cleaning formatted numbers and alongside test() for validating text-only input.
Remember where ^ negates versus anchors, use g when replacing all non-digits, and reach for \D when you want a shorter spelling of the same idea.
Assume Unicode numerals are excluded without testing
Use [^0-9] when you actually need [0-9]
Place ^ away from the start unless you mean literal caret
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about RegExp [^0-9]
Match and exclude non-digit characters in JavaScript.
5
Core concepts
🚫01
Negate
[^0-9]
Syntax
🔢02
Not digit
Letters, symbols.
Meaning
🔄03
\D
Shorthand.
Alias
🛠04
Strip
replace g.
Pattern
⚠05
^ context
[ ] vs anchor.
Pitfall
❓ Frequently Asked Questions
[^0-9] is a negated character class. The caret ^ immediately after the opening bracket inverts the set, so the pattern matches any single character that is NOT a digit (0 through 9).
Yes. In JavaScript, \D and [^0-9] both match a single non-digit ASCII character. They are interchangeable for basic use cases.
Not inside brackets. ^ only negates when it is the first character after [. Outside brackets, ^ still anchors to the start of the string as usual.
Use replace with the global flag: str.replace(/[^0-9]/g, ""). Every character matched by [^0-9] is removed, leaving digits only.
[0-9] matches a digit. [^0-9] matches anything except a digit—letters, spaces, punctuation, and symbols.
Negated character classes are core regex syntax since ES3. [^0-9] works in every browser and Node.js version.
Did you know?
The same negation trick works for any class: [^abc] matches anything except a, b, or c, and [^a-z] matches anything except lowercase letters. The caret must be the first character after the opening bracket.