JavaScript RegExp [^0-9] Character Class

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

What You’ll Learn

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

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.

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.

How to Use [^0-9] in JavaScript

The most common pattern strips non-digits by replacing them with an empty string:

JavaScript
const raw = "(555) 123-4567";
const digits = raw.replace(/[^0-9]/g, "");  // "5551234567"

const hasLetter = /[^0-9]/.test("404");    // false
const chunks  = "a1b2".match(/[^0-9]+/g);  // ["a", "b"]

Use test() to detect non-digit content, match() to extract letter runs, and replace(/[^0-9]/g, "") to keep digits only.

📝 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

Common patterns

  • /[^0-9]/ — detect any non-digit character.
  • /[^0-9]+/g — extract consecutive non-digit sequences.
  • /^[^0-9]+$/ — entire string has no digits (letters/symbols only).
  • /\D/g — compact form of /[^0-9]/g.

⚡ Quick Reference

GoalPattern
One non-digit[^0-9] or \D
One or more non-digits[^0-9]+
Keep digits onlystr.replace(/[^0-9]/g, "")
No digits in string/^[^0-9]*$/ or !/[\d]/.test(s)
Opposite (digit)[0-9] or \d
Negate other sets[^abc], [^a-z]

📋 [^0-9] vs [0-9] vs ^ anchor

The caret means different things depending on where it appears in the pattern.

Digit
[0-9]

Matches 0–9

Not digit
[^0-9]

Excludes 0–9

Shorthand
\D / \d

Non / digit

Anchor
^start

Outside [ ]

Examples Gallery

Open DevTools Console (F12) or use Try-it links. Examples cover detection, extraction, digit-only cleaning, and validation.

📚 Getting Started

Detect whether text contains non-digit characters.

Example 1 — Test for Non-Digit Characters

Use test() to see if a string contains anything other than digits.

JavaScript
const nonDigit = /[^0-9]/;

console.log(nonDigit.test("12345"));  // false
console.log(nonDigit.test("12a45"));  // true
console.log(nonDigit.test("hello"));  // true
Try It Yourself

How It Works

A string of digits only never satisfies [^0-9]. As soon as a letter or symbol appears, test() returns true.

📈 Practical Patterns

Extract, clean, and validate with negated classes.

Example 2 — Find the First Non-Digit with match()

Locate punctuation or letters inside mostly numeric text.

JavaScript
const code = "404NotFound";

const first = code.match(/[^0-9]/);
console.log(first[0]);  // "N"

const all = code.match(/[^0-9]/g);
console.log(all);       // ["N", "o", "t", "F", "o", "u", "n", "d"]
Try It Yourself

How It Works

Each [^0-9] matches one character outside the digit set. The global flag collects every letter in the HTTP-style status label.

Example 3 — Strip Formatting and Keep Digits Only

Remove parentheses, spaces, and dashes from a phone number.

JavaScript
const formatted = "(555) 123-4567";

const plain = formatted.replace(/[^0-9]/g, "");
console.log(plain); // "5551234567"

// Same idea with \D shorthand:
const plain2 = formatted.replace(/\D/g, "");
console.log(plain2); // "5551234567"
Try It Yourself

How It Works

Every non-digit matched by [^0-9] is replaced with nothing. The global flag ensures all formatting characters are removed, not just the first.

Example 4 — Extract Letter Sequences with [^0-9]+

Pull non-digit chunks from mixed alphanumeric data.

JavaScript
const log = "order1042shipped2024";

const words = log.match(/[^0-9]+/g);
console.log(words); // ["order", "shipped"]
Try It Yourself

How It Works

+ groups consecutive non-digits into whole tokens. Digits act as separators between the letter runs you extract.

Example 5 — Validate a Username With No Digits

Reject input that contains any numeric character.

JavaScript
function hasNoDigits(name) {
  return !/[0-9]/.test(name);
}

console.log(hasNoDigits("codetofun"));  // true
console.log(hasNoDigits("user42"));     // false

// Alternative: require only non-digits for the whole string
const lettersOnly = /^[^0-9]+$/;
console.log(lettersOnly.test("hello")); // true
Try It Yourself

How It Works

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.

🚀 Common Use Cases

  • Phone/card cleanupreplace(/[^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.

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.

📝 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.
  • Next topic: [^abc] negates a custom letter set.
  • Review [0-9] for the positive digit class counterpart.
  • In character classes, most metacharacters lose special meaning except ^, -, ], and \.

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 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
[^0-9] Excellent

Bottom line: Safe everywhere. Use [^0-9] or \D confidently for ASCII non-digit matching in all JavaScript runtimes.

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.

💡 Best Practices

✅ Do

  • Use replace(/[^0-9]/g, "") to normalize numbers
  • Put ^ first inside brackets to negate
  • Prefer !/[0-9]/.test(s) for “no digits” checks
  • Add + when you need consecutive non-digit runs
  • Know that \D equals [^0-9] in JS

❌ Don’t

  • Confuse negation [^0-9] with anchor ^
  • Forget the g flag when stripping all formatting
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about RegExp [^0-9]

Match and exclude non-digit characters in JavaScript.

5
Core concepts
🔢 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.

Continue to [^abc] negated class

Learn how to exclude specific letters from a custom character set using the same negation syntax.

[^abc] 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