Regex for numbers only
^\d+$ matches a string made only of digits, one or more. \d is the same as [0-9] in JavaScript; in Python 3 and .NET it also matches other Unicode digits unless you write [0-9]. To remove everything that is not a digit, replace \D with an empty string using the g flag.
Syntax
/^\d+$/Examples
["12345", "12a45", "", "007"].map(s => /^\d+$/.test(s))Result: [true, false, false, true]
The empty string fails because + needs at least one digit.
"(555) 123-4567".replace(/\D/g, "")Result: "5551234567"
Keep only the digits.
"Order 66 shipped 12 items".match(/\d+/g)Result: ["66", "12"]
["-12.50", "3", "1.", ".5"].map(s => /^-?\d+(?:\.\d+)?$/.test(s))Result: [true, true, false, false]
Optional sign and optional decimal part; digits are required on both sides of the point.
Common mistakes
- Using \d* which also matches the empty string; use \d+ when at least one digit is required.
- Converting to Number before validating, which turns 007 into 7 and accepts 1e3; test the string first.
- Forgetting that \d matches Arabic-Indic and other digits in Python 3; use [0-9] for ASCII only.
- Testing without anchors, so abc123 passes because it contains digits.
Practice Regex for numbers only
These drills check your answer on live data, free, no account needed.
- Keep only the digitsRemove every character that is not a digit, so phone numbers in any format become plain digits.
- One or more: goal, gooal, goooalThe whole line must match. Match g, then one or more o characters, then al. So goal, gooal and goooooal match, but gal does not.
- Currency amounts with commasThe whole line must match. Match a dollar amount: $, then 0 or a number without leading zeros that uses a comma every three digits (so $1,234, never $1234), then optionally a period and exactly two digits of cents.
Questions
What is the regex for a number with decimals?
^-?\d+(?:\.\d+)?$ for optional sign and decimals. To also allow .5, use ^-?(?:\d+\.?\d*|\.\d+)$.
How do I allow only numbers in an input field with regex?
Test the value against ^\d*$ on each change (the * allows the field to be empty while typing), or strip non-digits with value.replace(/\D/g, "").
How do I match a number between 1 and 100?
^(?:[1-9]\d?|100)$ : one or two digits not starting with 0, or exactly 100. Ranges in regex are built per digit length.
Related
- Regex for letters onlyThe ^[a-zA-Z]+$ pattern for ASCII letters, a Unicode version with \p{L} and the u flag, allowing spaces between words, and stripping non-letters from text.
- Regex for alphanumericThe ^[a-zA-Z0-9]+$ pattern, how it differs from \w, how to make a URL slug by replacing everything else, and a Unicode version with \p{L} and \p{N}.
- Regex for whitespaceHow \s matches spaces, tabs and newlines, trimming leading and trailing whitespace, collapsing repeated spaces, splitting on whitespace, and detecting blanks.
- QuantifiersWhat * + ? {n} {n,} and {n,m} mean, how greedy matching works, how to repeat a group instead of a single character, and the silent {2, 3} mistake.