The [0-9] character class matches any single digit from 0 through 9. It is one of the most common building blocks in JavaScript regex—used for parsing numbers, validating codes, and extracting digits from mixed text.
01
Class
[0-9]
02
One digit
0, 1, … 9
03
Like \d
ASCII digits
04
Quantifiers
+, {n}
05
Methods
test, match
06
Negate
[^0-9]
Fundamentals
Introduction
Regular expressions let you describe text patterns. A character class (text inside square brackets) matches one character from a set you define. The range [0-9] is shorthand for “any digit.”
You will see it in phone validators, form field checks, log parsers, and anywhere JavaScript needs to find or verify numeric characters inside strings.
Concept
Understanding the [0-9] Character Class
Inside [ and ], a hyphen between two characters creates a range. 0-9 includes every digit character. The pattern matches exactly one digit per [0-9] token unless you add a quantifier.
This is a character class, not a capture group. Parentheses ( ) create capturing groups; square brackets [ ] define which single character may appear at that position.
💡
Beginner Tip
To match a sequence of digits (like 2024), combine the class with + (one or more): /[0-9]+/. One [0-9] alone only consumes a single digit character.
Usage
How to Use [0-9] in JavaScript
Write the pattern as a literal or pass it to the RegExp constructor, then call standard methods:
[^0-9] matches every character that is not a digit. Replacing those with "" leaves digits only. Replacing digits with * masks them for display.
Applications
🚀 Common Use Cases
Form validation — ensure fields contain digits (age, ZIP code, quantity).
Log parsing — extract IDs, timestamps, and numeric metrics from text logs.
Input sanitization — strip formatting characters from phone or card numbers.
Password rules — require at least one digit with a lookahead or simple test().
Data cleaning — pull numeric values from CSV-like strings before conversion.
Masking — hide digits in UI output for privacy.
Watch Out
Important Considerations
One vs many — bare [0-9] matches a single digit; add + or {n} for sequences.
ASCII digits only — in JavaScript, [0-9] and \d match 0–9 only, not other numeral systems (e.g. Arabic-Indic digits) unless you use Unicode-aware patterns.
Anchors matter — without ^ and $, [0-9]+ matches a substring anywhere in the text.
Hyphen placement — in other positions inside [ ], a hyphen can be literal; at the start or end it is safe: [-0-9] or [0-9-].
Not a number type — regex returns strings; use Number() or parseInt after extraction.
🧠 How [0-9] Matches Text
1
Build the class
Square brackets define a set. The range 0-9 expands to all ten digit characters.
Pattern
2
Scan the string
The engine walks the input left to right looking for a position where the next character is in the set.
Search
3
Apply quantifier
If you wrote + or {4}, the engine repeats the digit match that many times.
Repeat
4
🔢
Return result
Match found → true or matched text. No digit → null or false.
Important
📝 Notes
[0-9] is equivalent to [0123456789] and to \d in JavaScript.
Use [^0-9] (or \D) for “anything except a digit.”
Combine with flags: /[0-9]/g for all digits, /[0-9]/i has no effect on digits.
For decimal numbers, consider /[0-9]+(\.[0-9]+)?/ to allow an optional fractional part.
See also RegExp exec() for reading capture groups from complex patterns.
The [0-9] character class is part of core JavaScript regular expression syntax and works identically across environments.
✓ Baseline · ES3+
RegExp [0-9]
Supported in every browser and Node.js version. No polyfill needed for basic digit character classes.
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 digit matching in all JavaScript runtimes.
Wrap Up
Conclusion
The [0-9] character class is a fundamental regex tool for matching digits in JavaScript. Combined with quantifiers and anchors, it powers validation, extraction, and cleaning tasks across web apps.
Start with /[0-9]/ for a single digit, /[0-9]+/g to find number runs, and /^[0-9]{n}$/ for exact-length numeric codes. When you need non-digits, reach for [^0-9] next.
Convert extracted strings with Number() when needed
Test edge cases: empty input, letters only, mixed text
❌ Don’t
Assume [0-9] matches an entire number alone
Forget the g flag when you need all matches
Confuse [0-9] (class) with (0-9) (invalid/literal group)
Expect Unicode numerals to match without explicit ranges
Over-complicate simple digit checks with heavy regex
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about RegExp [0-9]
Match digits confidently in JavaScript patterns.
5
Core concepts
📝01
Class
[0-9]
Syntax
🔢02
One digit
Per token.
Scope
🔄03
Like \d
Same in JS.
Shorthand
➕04
+ quantifier
Full numbers.
Pattern
🚫05
[^0-9]
Not a digit.
Negate
❓ Frequently Asked Questions
[0-9] is a character class that matches exactly one ASCII digit—any character from 0 through 9. Place it inside square brackets in your pattern: /[0-9]/.
In JavaScript, \d and [0-9] both match ASCII digits 0–9. They are interchangeable for basic digit matching. Use whichever reads clearer in your pattern.
By itself, [0-9] matches only one digit. Add a quantifier like + (one or more) or {4} (exactly four) to match longer number sequences: /[0-9]+/ or /[0-9]{4}/.
[0-9] matches a digit. [^0-9] matches any single character that is NOT a digit—the ^ inside brackets negates the class.
Yes. All standard RegExp methods work with character classes. test() checks for a match, match() extracts matches, and replace() substitutes matched digits.
Character classes have been part of JavaScript regular expressions since ES3. [0-9] works in every browser and Node.js version.
Did you know?
Character classes like [0-9], [a-z], and [abc] match exactly one character per class token. To match a literal hyphen inside a class, put it first or last: [-09] or [0-9-].