Regex for Numbers Only (Digits)
^\d+$ accepts a string made entirely of digits: no spaces, signs, decimal points or letters. The anchors are what make it 'only' — without them, \d+ would happily find the 123 inside abc123.
/^\d+$/How it works, token by token
| Token | Meaning |
|---|---|
| ^ | start of string |
| \d+ | one or more digits (0–9) |
| $ | end of string — nothing else may follow |
What it matches
✓
0✓
42✓
0012345✗
✗
12a✗
-5✗
3.14✗
1 000Try it live
One candidate per line — the m flag makes the ^ and $ anchors apply to each line. Edit anything; it runs in your browser.
/
Variations
^\d{4}$exactly four digits, such as a PIN or a year^\d*$digits or an empty string^[0-9]+$ASCII digits only, in every languageLanguage notes
- In Python 3 and .NET, \d also matches other scripts' digits, like ٣ or ३. Use [0-9], or Python's re.ASCII flag, to accept only 0–9.
Frequently asked questions
How do I allow negative numbers?
Add an optional sign: ^-?\d+$. For decimals as well, see the decimal number pattern.
Should I validate numbers with regex at all?
For form input it's fine. If you need the value, parse it (Number.parseInt, int()) and check its range too — a regex can't tell you a number is under 1,000.
Related patterns
Building something custom? The regex tester gives you live match highlighting for any pattern.