JavaScript RegExp [^0-9] Character Class

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is [^0-9]?

[^0-9] is a negated character class. It matches exactly one character that is not an ASCII digit 0–9. The ^ must be the first character inside the brackets to mean “not.”

Remember
/[^0-9]/     → one non-digit (letter, space, symbol, …)
/[0-9]/      → one digit (the opposite class)
/[^0-9]+/    → one or more non-digits
/\D/         → same idea as [^0-9] (shorthand)

"A1!".match(/[^0-9]/g)  → ["A", "!"]

^ inside []  → negate the class
^ outside [] → start-of-string anchor
UsePrefer
Match a non-digit/[^0-9]/ or /\D/
Match a digit/[0-9]/ or /\d/
Keep digits onlystr.replace(/[^0-9]/g, "")

How to Use [^0-9]

Write the class in a regex, then use test, match, or replace.

  1. Write /[^0-9]/ (add g to find every match).
  2. Use test() to check if any non-digit exists.
  3. Use match() to collect non-digit characters.
  4. Use replace(/[^0-9]/g, "") to strip non-digits and keep numbers.
PieceRequired?Role
[^0-9]YesOne character that is not 0–9
Quantifier (+, *, …)OptionalMatch a run of non-digits
Flag gOptionalFind / replace all matches

Pseudocode

Pseudocode
pattern = [^0-9]     // "not digit 0–9"

for each character c in text:
    if c is NOT in {0,1,2,3,4,5,6,7,8,9}:
        c matches
    else:
        c does not match

// with + : match the longest run of such characters

Cheat Sheet

Keep these facts handy while writing digit / non-digit patterns.

TopicDetail
Syntax/[^0-9]/
MeaningOne character that is not 0–9
Shorthand\D (same for basic ASCII digits)
Opposite[0-9] or \d
One or more/[^0-9]+/
Strip non-digitsstr.replace(/[^0-9]/g, "")
StandardCore regex (ES3+) — universal support

Live Preview

Pick a sample string and see every match of /[^0-9]/g.

Browser demo of /[^0-9]/g — same idea as the examples below.

Result
text = "A1b2!"
text.match(/[^0-9]/g)
→ ["A", "b", "!"]

/* digits skipped; non-digits listed */

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Detect Non-Digits with test()

Check whether a string contains any character that is not a digit.

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

  1. Build the pattern. /[^0-9]/ means “one non-digit.”
  2. Digits only. "12345" has no match → false.
  3. Letter in the middle. "a" matches → true.
  4. All letters. Every character matches → true.

Example 2 — Collect Non-Digits with match()

Use the global flag to list every non-digit character.

JavaScript
const text = "A1b2!";

console.log(text.match(/[^0-9]/g));   // ["A", "b", "!"]
console.log(text.match(/[^0-9]+/g));  // ["A", "b", "!"]
console.log("123".match(/[^0-9]/g)); // null
Try It Yourself

How It Works

  1. Global scan. /g finds every separate non-digit.
  2. One at a time. [^0-9] returns each match as its own string.
  3. With +. Here non-digits are isolated, so + gives the same list.
  4. No matches. Digits-only input → null from match.

Example 3 — Strip Non-Digits (Keep Numbers)

Remove everything that is not 0–9 — same idea as \D.

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

console.log(phone.replace(/[^0-9]/g, ""));  // "5551234567"
console.log(phone.replace(/\D/g, ""));      // "5551234567"
console.log(/[^0-9]/.source);               // "[^0-9]"
Try It Yourself

How It Works

  1. Formatted input. Parentheses, spaces, and dashes are non-digits.
  2. Global replace. Every [^0-9] match is replaced with "".
  3. Same with \D. /\D/g produces the same cleaned number string.
  4. Common use. Normalize phone numbers or IDs before storing or comparing.

Edge Cases & Pitfalls

Check these before relying on [^0-9] in real projects.

trap

^ inside vs outside brackets

Inside [] at the start, ^ negates. Outside, ^ is the start anchor. /^[0-9]/ means “digit at start,” not “non-digit.”

one

Matches one character by default

Add + or * when you need a whole run of non-digits, not a single character.

ascii

ASCII digits only

[0-9] / [^0-9] target ASCII 0–9. Other numeral systems need Unicode-aware patterns.

null

match returns null when nothing fits

Digits-only strings yield null for /[^0-9]/g. Guard with || [] if you always want an array.

Browser & Runtime Support

Negated character classes like [^0-9] are core JavaScript regular-expression syntax (ES3+) and work everywhere.

✓ Baseline · ES3+

RegExp [^0-9]

Supported in all evergreen browsers, Internet Explorer, and every current Node.js / Deno / Bun release.

100% Universal support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 4+ · Legacy environments
Full support
Opera All 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] Universal

Bottom line: Safe everywhere. Use [^0-9] or \D for non-digits; use [0-9] or \d for digits.

Performance Notes

ApproachCostBest for
/[^0-9]/.test(s)O(n)Quick “has non-digit?” check
s.match(/[^0-9]/g)O(n)List every non-digit
s.replace(/[^0-9]/g, "")O(n)Keep digits only

Character-class matching is linear in string length for these patterns. Prefer test when you only need a yes/no answer.

Key Takeaways

  • Role: [^0-9] matches one character that is not a digit.
  • Twin: \D is the same idea for basic ASCII digits.
  • Strip: replace(/[^0-9]/g, "") keeps digits only.
  • Caret: ^ inside [] negates; outside it anchors the start.

One line: [^0-9] means “not a digit” — the inverse of [0-9], and a clear twin of \D.

Frequently Asked Questions

[^0-9] is a negated character class that matches exactly one character that is NOT an ASCII digit 0–9. The caret ^ inside the brackets means “not these characters.”
Yes, for standard JavaScript regex without the Unicode (u) flag. Both match one non-digit. Prefer whichever reads clearer in your pattern.
By itself it matches one character. Add a quantifier such as + (one or more) or * (zero or more): /[^0-9]+/ matches a run of non-digits.
[0-9] matches a digit. [^0-9] matches any single character that is not a digit. The leading ^ inside [] negates the class.
Use replace with the global flag: str.replace(/[^0-9]/g, ""). Every non-digit is removed, leaving digits only.
No. Outside brackets, ^ means start of string (or line with m). Inside brackets at the start of the class, ^ means “not.”

Next: [^abc]

Learn negated character classes for a custom set of characters — not just digits.

[^abc] tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

13 people found this page helpful