JavaScript - integer regular expression

5 points
Created by:

In this short article, we would like to show how in a simple way check if the text is an integer number using regular expressions in JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

const expression = /^[+-]?\d+$/;

const isInteger = (text) => !!text.match(expression);

// Usage example:

console.log(isInteger('123'));      // true
console.log(isInteger('-123'));     // true
console.log(isInteger('+123'));     // true
console.error(isInteger('3.14'));   // false
console.error(isInteger('abc'));    // false
console.error(isInteger('a123b'));  // false

See also

  1. JavaScript - float regular expression

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂