Quick answer
The regex for numbers only is ^\d+$: one or more digits from start to end, so 42 and 007 match while -5, 3.14, 12a and an empty string do not. In JavaScript \d means only the ASCII digits 0 to 9; in Python and .NET it also matches digits from other scripts, so use [0-9] there when you mean plain ASCII digits.
- Tested examples
- Token-by-token breakdown
- Code in 5 languages
- Live tester
How to use the numbers only (digits) regex
- 1Copy the pattern with Copy pattern in the tester, or take a ready-made snippet for your language from the code section below.
- 2Check the sample lines in Test string: every line the numbers only (digits) regex accepts is highlighted, rejected lines stay plain.
- 3Paste your own values one per line to see which pass, and read Explanation for what each part of the pattern does.
- 4Open the Code tab for JavaScript, Python, Go, C#, PHP or Dart with the pattern translated and escaped.
- 5Validate again on the server: client-side checks give users quick feedback, not security.
How does the numbers only (digits) regex work?
Read it left to right. Each piece below matches one part of the input:
^Start of the string.\d+One or more digits. In JavaScript, \d is exactly [0-9].$End of the string, so nothing else may follow.
Which strings does the numbers only (digits) regex match?
The whole input must match, from the first character to the last. Every row below is checked against the pattern by our automated test suite before each release.
| Input | Matches? | Why |
|---|---|---|
0 | Yes | A single digit. |
42 | Yes | An ordinary number. |
007 | Yes | Leading zeros are digits too (useful for codes and IDs). |
1234567890 | Yes | Any length. |
-5 | No | The minus sign is not a digit (see the variants below). |
3.14 | No | Neither is the decimal point. |
12a | No | A letter at the end. |
| (empty string) | No | Empty input: + needs at least one digit. |
1 000 | No | A space as a thousands separator. |
١٢٣ | No | Arabic-Indic digits: not matched by JavaScript's \d. |
What about negative numbers, decimals and thousands separators?
Pick the variant that matches what you accept:
- Digits only:
^\d+$matches42and007. - Integers with an optional sign:
^[+-]?\d+$matches-5and+12. - Decimals:
^[+-]?(?:\d+\.?\d*|\.\d+)$matches3.14,.5and10.. - No leading zeros:
^(?:0|[1-9]\d*)$matches0and120but not007. - Thousands separators:
^\d{1,3}(?:,\d{3})*$matches1,234,567. - Scientific notation:
^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$matches6.02e23.
Separators and decimal marks depend on the locale: 1.234,5 in Germany is 1,234.5 in the US. For user input, parse with Intl.NumberFormat-aware code or a library rather than adding every convention to the regex.
Does \d match digits in other scripts?
It depends on the engine, and the difference causes real bugs:
- JavaScript and Go:
\dis[0-9], even with theuflag. - Python 3 (
strpatterns), .NET and Java with UNICODE_CHARACTER_CLASS:\dmatches any Unicode decimal digit, including Arabic-Indic٣, Devanagari३and full-width3.
So ^\d+$ in Python accepts "١٢٣", and then int("١٢٣") returns 123, which may or may not be what you want. Use [0-9] (or the re.ASCII flag) when you mean ASCII digits, and \p{Nd} with the u flag in JavaScript when you genuinely want every script.
For an HTML form, inputmode="numeric" shows a number pad without the quirks of type="number" (which accepts e, + and -).
How do I use the numbers only (digits) regex in JavaScript, Python, Go, C# and PHP?
The pattern is translated for each engine rather than pasted as is: named groups, anchors, escapes, shorthand classes and flags are adjusted so each snippet behaves as closely to the JavaScript version as its engine allows. Differences that remain (Go's RE2 has no lookarounds or backreferences, for example) are called out under each one. The Code tab in the tester above also exports Dart, and updates as you edit the pattern.
JavaScript and TypeScript
const regex = /^\d+$/;
function isValid(value) {
return regex.test(value);
}
console.log(isValid("0"));Python
import re
pattern = re.compile(r"^[0-9]+\Z")
def is_valid(value: str) -> bool:
return pattern.search(value) is not None
print(is_valid("0"))- \d and \w are written as [0-9] and [A-Za-z0-9_] because in Python they also match digits and letters from other scripts.
- JavaScript's $ only matches at the very end of the string. Python's $ also matches before a trailing newline, so the pattern uses \Z instead.
Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^\d+$`)
fmt.Println(re.MatchString("0"))
}C#
using System;
using System.Text.RegularExpressions;
// The timeout stops a pathological input from hanging the thread (ReDoS).
var regex = new Regex(@"^[0-9]+\z", RegexOptions.None, TimeSpan.FromSeconds(1));
Console.WriteLine(regex.IsMatch("0"));- \d and \w are written as [0-9] and [A-Za-z0-9_] because in .NET they also match digits and letters from other scripts.
- JavaScript's $ only matches at the very end of the string. .NET's $ also matches before a trailing newline, so the pattern uses \z instead.
PHP
<?php
$pattern = '/^\d+$/D';
function isValid(string $value, string $pattern): bool
{
return preg_match($pattern, $value) === 1;
}
var_dump(isValid('0', $pattern));- The D modifier makes $ match only at the very end, like JavaScript (PCRE's $ also matches before a trailing newline).
Which related regex patterns are useful?
- Regex for Alphanumeric Characters: Letters A–Z and digits only, with a Unicode variant for every script.
- Regex for Phone Numbers (E.164 Format): International numbers in E.164: a plus, a country code and up to 15 digits in total.
- Regex for Credit Card Numbers: Visa, Mastercard, Amex, Discover, Diners and JCB by prefix and length (digits only).
- Regex for US ZIP Codes: Five-digit ZIP codes with an optional ZIP+4 extension, like 90210-1234.
Browse all 25 patterns in the regex pattern library, or build your own in the regex tester.
Frequently asked questions
Last updated by the Playxoft engineering team.