EN
JavaScript - integer regular expression
8
points
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