Regex for digits only

Use /^\d+$/ to check that a whole string is for digits only. Every example below is tested against the pattern, and you can open it in the Regex Tester with the samples preloaded.

Updated

InputResult
0✓ Match
123456✓ Match
007✓ Match
12a✗ No match
1.5✗ No match
 42✗ No match
-3✗ No match

How the pattern works

PartMeaning
^Start of the string
\d+One or more digits 0–9
$End of the string

Code examples

JavaScript

const re = /^\d+$/;
re.test(value); // true or false

Python

import re

bool(re.fullmatch(r'\d+', value))

PHP

preg_match('/^\d+$/', $value) === 1;

Notes and limitations

Use ^\d*$ to also allow an empty string. In JavaScript, \d only matches ASCII digits; in Python 3 it also matches other scripts’ digits (like ٣) unless you pass re.ASCII.

Regex TesterType a pattern and watch matches highlight live in your test text.
Open the tool

Frequently asked questions

What is the regex for digits only?
/^\d+$/ — it matches the whole string. See the table above for what each part does.
Which examples match?
Of the examples tested, these match: 0, 123456, 007. The others don’t.
How do I test this regex?
Click “Open in Regex Tester” — the pattern, flags and sample text load automatically, and matches highlight as you edit.