JavaScript / ECMAScript

Integer in JS

Matches whole integers, including negative numbers.

Try it in the JS tester →

Pattern

regexJS
^-?\d+$

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).

How the pattern 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

Same pattern, other engines

← Back to Integer overview (all engines)