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 match = input.match(re);
console.log(match ? match[0] : null);

Uses `String.prototype.match`, which returns the first match or null.

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