Languages
[Edit]
EN

JavaScript - integer regular expression

8 points
Created by:
chelsea
806

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

Alternative titles

  1. JavaScript - validate integer number (integer as text)
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
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