JavaScript RegExp [0-9] Character Class

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

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.

Remember
/[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
UsePrefer
One digit/[0-9]/ or /\d/
A whole number/[0-9]+/
Not a digit/[^0-9]/

How to Use [0-9]

Put the class in a pattern, add a quantifier if needed, then test or extract.

  1. Write [0-9] inside your regex for one digit.
  2. Add +, *, or {n} when you need more than one digit.
  3. Use test() for yes/no, or exec() / match() to read the digits.
  4. Prefer ^…$ when the whole string must be digits only (e.g. a PIN).
PieceRequired?Role
[0-9]YesMatches one digit
QuantifierOptional+, {4}, etc. for length
AnchorsOptional^ / $ for whole-string checks

Pseudocode

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)

Cheat Sheet

Keep these patterns handy while matching digits.

PatternMatches
/[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

Live Preview

Pick a sample string and see whether /[0-9]/ finds a digit.

Browser demo of /[0-9]/ with test and exec.

Result
str = "Room 7"
/[0-9]/.test(str) → true
exec → "7" (index 5)

JavaScript Examples

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

Example 1 — Match One Digit

Use test() and exec() with /[0-9]/.

JavaScript
const digit = /[0-9]/;

console.log(digit.test("Room 7"));  // true
console.log(digit.test("Hello"));   // false
console.log(digit.exec("A4B")[0]);  // "4"
Try It Yourself

How It Works

  1. Define the class. /[0-9]/ matches any one digit.
  2. Find a hit. "Room 7" contains 7 → true.
  3. No digits. "Hello" fails the test → false.
  4. Read the digit. exec("A4B") returns "4" as the match.

Example 2 — One or More Digits

Add + to grab a full number run.

JavaScript
const numbers = /[0-9]+/g;
const str = "Order 12 and 345";

console.log(str.match(numbers));
// ["12", "345"]
Try It Yourself

How It Works

  1. Quantifier +. [0-9]+ means one or more digits in a row.
  2. Global flag. g finds every number in the string.
  3. match. Returns ["12", "345"].
  4. 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.

JavaScript
const pin = /^[0-9]{4}$/;

console.log(pin.test("9081"));  // true
console.log(pin.test("42"));    // false
console.log(pin.test("90a1"));  // false
Try It Yourself

How It Works

  1. Exact count. {4} requires exactly four digits.
  2. Anchors. ^ and $ mean the whole string must match.
  3. Valid PIN. "9081" passes.
  4. 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 opposite

A leading ^ inside brackets means “not these characters.” See [^0-9].

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.

100% Universal support
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.

Performance Notes

ApproachCostBest for
/[0-9]/O(n) scanDetect any digit
/[0-9]+/gO(n) scanExtract 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.

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

[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.
Yes for basic ASCII digits. In JavaScript, \d and [0-9] both match 0–9. Use whichever reads clearer in your pattern.
By itself, [0-9] matches only one digit. Add a quantifier like + or {4}: /[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 for yes/no, exec for details, match to extract.
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 →

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.

9 people found this page helpful