What is a non-negative integer?
A A non-negative integer is "an integer that is either 0 or positive."
Source: http://mathworld.wolfram.com/NonnegativeInteger.html
In other words, you want to check for a non-negative integer.
There are not enough answers because they do not include integers, such as -0 and -0000 , which technically become non-negative integers after parsing. Other answers also do not confirm integers with + in front.
You can use the following regular expression to test:
/^(\+?\d+|-?0+)$/
Try it online!
Explanation:
^
The following test cases return true: -0 , -0000 , 0 , 00000 , +0 , +0000 , 1 , 12345 , +1 , +1234 . The following test cases return false: -12.3 , 123.4 , -1234 , -1 .
Note. This regular expression does not work for whole lines written in scientific notation.
Grant Miller Feb 27 '18 at 1:45 2018-02-27 01:45
source share