问题
Yesterday I was posting one question here Regular Expression allow null or 1 to 9 digit This was working fine after that I saw in my quantity field not taking 10, 100, 1002 etc. value. because these values also present 0(zero) digit. So Please help me.
回答1:
If you want null or a number that starts from [1-9]
but can contain [0-9]
, the pattern is
(^[1-9][0-9]*$)|(^$)
please, notice that a single zero is not allowed: 0
doesn't match. In case you want to match it as well (you want a empty string, zero - 0
or a number which must start from [1-9]
and can contain [0-9]
)
(^[1-9][0-9]*$)|(^0?$)
回答2:
I would do with this expression:
(^[1-9][0-9]*$)|(^$)
or even shorter using \d which means [0-9]:
(^[1-9]\d*$)|(^$)
来源:https://stackoverflow.com/questions/40377762/regular-expression-allow-zero-so-long-as-it-is-not-the-first-digit