Integer Regex
Validates a whole number with an optional leading minus sign, disallowing decimal points and unnecessary leading zeros.
Regex Pattern
^-?(0|[1-9]\d*)$Pattern Breakdown
Hover over a token to see what it does.
| Token | Meaning |
|---|---|
| ^ | Anchors the match to the start of the string. |
| -? | An optional leading minus sign for negative integers. |
| 0 | Matches the single digit zero on its own. |
| [1-9]\d* | A non-zero digit followed by any number of digits, so multi-digit numbers can't start with 0. |
| $ | Anchors the match to the end of the string. |
Detailed Explanation
What it does
This pattern matches strings representing a whole number, optionally negative, with no decimal point. It accepts a bare 0, or a non-zero digit followed by any further digits, so values like 42, -17, and 0 all match, while 007 and 3.14 do not.
Why it works
The alternation `(0|[1-9]\d*)` separates the single-character case of zero from multi-digit numbers, which must start with a non-zero digit. This prevents malformed leading-zero values like 007 from matching while still allowing arbitrarily long numbers such as 123456. The optional `-?` at the start handles negative integers without allowing a stray minus sign elsewhere in the string.
Common use cases
- Validating quantity or count fields in a form that should only accept whole numbers
- Filtering CSV columns or query parameters that must be plain integers
- Rejecting decimal or malformed numeric input before parsing with `parseInt` or similar
- Validating page numbers, IDs, or array indices in an API request
Edge cases
- -0 matches this pattern since the sign and the digit 0 are both individually valid, even though it's a slightly unusual value
- 007 correctly fails because leading zeros are disallowed for multi-digit numbers
- An empty string fails because at least one digit is required
- A number with surrounding whitespace, like ' 5', fails because the anchors require the whole string to be digits
Limitations
- Does not support a leading plus sign for explicitly positive integers (e.g. +5)
- Does not enforce any minimum or maximum value range, only the textual shape
- Does not support thousands separators like commas or underscores
Interactive Tester
Edit the pattern or text below — matching runs live in your browser.
Test Cases
Editable — add your own inputs to see if they pass.
| Input | Expected | Result | |
|---|---|---|---|
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass |
Language Variants
Production-ready examples in 12 languages.
const integerPattern = /^-?(0|[1-9]\d*)$/;
function isInteger(value) {
return integerPattern.test(value);
}
console.log(isInteger("-17")); // trueCommon Mistakes
Using a naive `^-?\d+$`, which accepts leading zeros like 007 that most systems consider malformed.
Fix: Use the `(0|[1-9]\d*)` alternation to allow a bare zero but forbid leading zeros on longer numbers.
Forgetting the optional minus sign, so valid negative integers get rejected.
Fix: Add `-?` right after the start anchor to allow an optional leading minus.
Relying on this regex alone to guard against integer overflow in a fixed-size type.
Fix: After the format passes, parse the value and check it against the target type's min/max range.
Performance Notes
- The pattern has no nested or overlapping quantifiers, so it runs in linear time with no catastrophic backtracking risk.
- Anchoring both ends lets non-numeric input fail immediately without scanning further.
- For very large numbers of validations, precompile the regex once and reuse it rather than recreating it per call.
Browser Compatibility
| Engine | Supported | Notes |
|---|---|---|
| Chrome | Yes | — |
| Firefox | Yes | — |
| Safari | Yes | — |
| Edge | Yes | — |
| Node.js | Yes | — |
Work with this pattern
Take this expression further — test it against your own data, adapt it in the builder, or read up on the syntax behind it.
Regex Tester
Test a regular expression against sample text and see every match highlighted live.
Regex Generator
Build a pattern by clicking blocks — no syntax memorisation required.
Regular Expressions Guide
What regex is, how the syntax works, and the mistakes that trip people up.
Regex Cheat Sheet
Every token, quantifier, and assertion on one printable page.
Python Regex Guide
The re module end to end — search, match, findall, groups, and substitution.
Regex Pattern Library
100+ ready-to-use patterns for email, URLs, dates, IPs, and more.