Numbers
Integer
Matches whole integers, including negative numbers.
Try it in RegexProPattern
regexJavaScript
/^-?\d+$/Raw source: ^-?\d+$
How it works
`^-?` optionally allows a leading minus sign. `\d+` requires one or more digits. `$` anchors to end.
Examples
Input
42Matches
42
Input
-17Matches
-17
Input
3.14No match
—Common use cases
- Form validation
- ID parsing
- Configuration values
Related concepts
Anchors: ^ and $ Explained
ConceptAnchors match positions, not characters. ^ asserts the start of the string (or line, with the m flag) and $ asserts the end.
Character Classes: \d, \w, \s and Their Negations
ConceptShorthand character classes match broad categories of characters. \d is a digit, \w is a word character, \s is whitespace. Uppercase negates.
Quantifiers: *, +, ?, and {n,m}
ConceptQuantifiers repeat the preceding element. * is 0 or more, + is 1 or more, ? is 0 or 1. {n}, {n,}, and {n,m} give exact counts and ranges.
How to Match Digits in Regex
How-toUse \d for any digit, [0-9] for ASCII digits only, or {n} to match a specific count of digits. Combine with anchors for whole-string validation.