Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript match numbers that are not zero or start with zero

I have the following regex that I'm trying to ONLY allow numbers like:

1, 2, 3, 10, 11, 24 etc

and NOT 0, 01, etc

if (!$(this).text().match(/^[1-9][0-9]/g)) {


}

Is this correct? As it doesn't allow numbers like 1, 2, 3 but is 11, 12 etc

like image 979
Cameron Avatar asked Aug 24 '26 09:08

Cameron


1 Answers

You need to specify a * after second [0-9] to match zero or more digits. In addition to one digit numbers, this will also fail to match more than two digit numbers. Correct regular expression is ^[1-9][0-9]*.

like image 139
taskinoor Avatar answered Aug 26 '26 22:08

taskinoor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!