Regex to reject specific values

To allow all integers, except 0 or a number with a leading zero, you can use the following regular expression:

^[1-9]\d*$

Here is a live example: https://regex101.com/r/DauRoh/1


This approach is only reasonable, if you have strings, which are representing numbers. In cases, where you have an integer number, you can check if the number is larger then 0. Leading zeros are not a problem then.

num = 123;

if(num > 0) {
   console.log('greater than 0');
} else {
   console.log('lower or equal to 0');
}

Leave a Comment