Numbers

Integer

Matches whole integers, including negative numbers.

Try it in RegexPro →

Available in

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("^-?\\d+$", "");
const input = "42";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

Pattern

regexengine-agnostic
^-?\d+$

Raw source: ^-?\d+$

How it works

`^-?` optionally allows a leading minus sign. `\d+` requires one or more digits. `$` anchors to end.

Examples

Input

42

Matches

  • 42

Input

-17

Matches

  • -17

Input

3.14

No match

Common use cases

  • Form validation
  • ID parsing
  • Configuration values

Related patterns

Related concepts