Languages
[Edit]
EN

JavaScript - integer regular expression

5 points
Created by:
chelsea
356

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

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.

❤️💻 🙂

Join