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:


Here is a live example:

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