Home JavaScript RegExp [0-9] JavaScript RegExp [0-9] Character Class Definition
What Is [0-9]? [0-9] is a character class that matches one ASCII digit from 0 through 9. In JavaScript it matches the same digits as \d .
/[0-9]/ → one digit (0…9)
/[0-9]+/ → one or more digits
/[0-9]{4}/ → exactly four digits
same idea as /\d/ in JavaScript
[^0-9] → one non-digit Use Prefer One digit /[0-9]/ or /\d/A whole number /[0-9]+/Not a digit /[^0-9]/
Approach
How to Use [0-9] Put the class in a pattern, add a quantifier if needed, then test or extract.
Write [0-9] inside your regex for one digit. Add +, *, or {n} when you need more than one digit. Use test() for yes/no, or exec() / match() to read the digits. Prefer ^…$ when the whole string must be digits only (e.g. a PIN). Piece Required? Role [0-9]Yes Matches one digit Quantifier Optional +, {4}, etc. for lengthAnchors Optional ^ / $ for whole-string checks
Pseudocode pattern = /[0-9]/ // or /[0-9]+/, /[0-9]{n}/
str = text to check
if pattern.test(str):
// at least one digit found (for /[0-9]/)
match = pattern.exec(str)
// match[0] is the digit (or digit run) Reference
Cheat Sheet Keep these patterns handy while matching digits.
Pattern Matches /[0-9]/One digit /[0-9]+/One or more digits /[0-9]{3}/Exactly three digits /^[0-9]+$/Whole string is digits only /\d/Same as [0-9] in JS /[^0-9]/One non-digit
Try it
Live Preview Pick a sample string and see whether /[0-9]/ finds a digit.
Code
JavaScript Examples Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
1. One digit 2. One or more 3. Exact length Example 1 — Match One Digit Use test() and exec() with /[0-9]/.
const digit = /[0-9]/;
console.log(digit.test("Room 7")); // true
console.log(digit.test("Hello")); // false
console.log(digit.exec("A4B")[0]); // "4" How It Works Define the class. /[0-9]/ matches any one digit.Find a hit. "Room 7" contains 7 → true.No digits. "Hello" fails the test → false.Read the digit. exec("A4B") returns "4" as the match.Example 2 — One or More Digits Add + to grab a full number run.
const numbers = /[0-9]+/g;
const str = "Order 12 and 345";
console.log(str.match(numbers));
// ["12", "345"] How It Works Quantifier +. [0-9]+ means one or more digits in a row.Global flag. g finds every number in the string.match. Returns ["12", "345"].Not just one char. Without +, you would only get single digits.Example 3 — Exact Four Digits (PIN) Anchor the pattern so the whole string must be four digits.
const pin = /^[0-9]{4}$/;
console.log(pin.test("9081")); // true
console.log(pin.test("42")); // false
console.log(pin.test("90a1")); // false How It Works Exact count. {4} requires exactly four digits.Anchors. ^ and $ mean the whole string must match.Valid PIN. "9081" passes.Rejects. Too short ("42") or non-digits ("90a1") fail. Edge Cases & Pitfalls Check these before validating numbers with [0-9].
length One digit by default [0-9] alone never matches "12" as a whole — use + or {n}.
unicode ASCII digits only [0-9] (and JS \d) match 0–9, not other Unicode numeral characters.
trap Hyphen is a range Inside [ ], 0-9 means a range. That is intentional here — do not confuse it with a literal hyphen.
negate [^0-9] is the oppositeA leading ^ inside brackets means “not these characters.” See [^0-9] .
Compatibility
Browser & Runtime Support Character classes like [0-9] are a core part of JavaScript regular expressions — available in every environment.
✓ Baseline · Widely available
RegExp [0-9] Supported in all browsers and Node.js versions since early ECMAScript. No polyfill needed.
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] Universal
Bottom line: Use freely. Pair with quantifiers and anchors when you need full numbers or exact-length codes.
Analysis
Performance Notes Approach Cost Best for /[0-9]/O(n) scan Detect any digit /[0-9]+/gO(n) scan Extract all numbers /^[0-9]{n}$/O(n) Fixed-length digit strings
Digit classes are cheap. Prefer a clear pattern ([0-9] or \d) over hand-rolled loops when validating or extracting numbers.
Remember
Key Takeaways Role: [0-9] matches one ASCII digit 0–9.
Length: add + or {n} for multi-digit numbers.
Alias: in JS, \d matches the same digits as [0-9].
Whole string: use ^[0-9]{n}$ for exact codes / PINs.
One line: [0-9] is one digit — add quantifiers and anchors when you need full numbers or exact-length codes.
Frequently Asked Questions What does [0-9] mean in a JavaScript regular expression? [0-9] is a character class that matches exactly one ASCII digit — any character from 0 through 9. Write it as /[0-9]/ in a pattern.
Is [0-9] the same as \d in JavaScript? Yes for basic ASCII digits. In JavaScript, \d and [0-9] both match 0–9. Use whichever reads clearer in your pattern.
Does [0-9] match multiple digits at once? By itself, [0-9] matches only one digit. Add a quantifier like + or {4}: /[0-9]+/ or /[0-9]{4}/.
What is the difference between [0-9] and [^0-9]? [0-9] matches a digit. [^0-9] matches any single character that is not a digit — the ^ inside brackets negates the class.
Can I use [0-9] with test(), exec(), and match()? Yes. All standard RegExp methods work with character classes — test for yes/no, exec for details, match to extract.
Which browsers support [0-9]? Character classes have been in JavaScript since early ES. [0-9] works in every browser and Node.js version.
Next: [abc] Learn character sets that match one character from a list.
[abc] tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion