Regex for Integer
Matches positive and negative whole numbers (integers). No decimals, leading zeros allowed.
Pattern
/^-?\d+$/Pattern Breakdown
| Token | Explanation |
|---|---|
| ^ | Start of string |
| -? | Optional minus sign |
| \d+ | One or more digits |
| $ | End of string |
Test Examples
| Input | Match? |
|---|---|
| 42 | Match |
| -100 | Match |
| 0 | Match |
| 3.14 | No match |
| abc | No match |
Common Use Cases
- Numeric input validation
- Form field checking
- Data type enforcement
Variations
^[1-9]\d*$Positive integers without leading zeros
^(0|[1-9]\d*)$Non-negative integers
Other Regex Patterns
Want to test this pattern with your own data?
Open the Regex Tester