简体   繁体   English

正则表达式在某些情况下允许值1到100

[英]Regex to allow values 1 to 100 with some conditions

i want a regex to allow 1-3 digits whole number,it should not start with 0 and should be a whole number,not decimals or fractions or any signed values, 我希望正则表达式允许1-3位整数,但不能以0开头,而应为整数,不能为小数或小数或任何带符号的值,

this is what i have tried 这就是我尝试过的

"/^(\s*|\d{1,3})$/"

but it does not seem to work.. 但它似乎不起作用。

thanks in advance 提前致谢

You can do it like this: 您可以这样做:

^(([1-9]\d?)|100)$

This would ensure that 这样可以确保

  • There is at least one digit 至少有一位数字
  • That multi-digit numbers start in a digit other than zero 多位数字以非零的数字开头
  • That the only allowed three-digit number is 100. 唯一允许的三位数是100。
^[1-9][0-9]\{,2\}$

It should be indeed end of line; 它确实应该是行尾; end-of word will get the fractional part. 词尾将得到小数部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM