简体   繁体   English

有或没有前导零而不是一个尾随零的小数的正则表达式是什么?

[英]What is the regex for decimals with or without leading zeros, and not one trailing zero?

I have this regex (for up to 3 numbers after the decimal): 我有这个正则表达式(小数点后最多3个数字):

/^(\d*[1-9]\d*(\.\d{1,3})?|0*\.\d{1,3})$/

I'm hoping to set these as valid : 我希望将它们设置为有效

  • 0 0
  • 0.01 0.01
  • 0.1 0.1
  • 12 12
  • 12.01 12.01
  • 12.1 12.1
  • 12.420 12.420

And these as invalid : 而这些无效

  • 12.0 12.0
  • 0.0 0.0
  • 012 012

This seems to do the trick 这似乎可以解决问题

^(0|[1-9]\d*)(\.\d{0,2}[1-9])?$

https://regex101.com/r/6ZKH3f/1/ https://regex101.com/r/6ZKH3f/1/

To only disallow .0 and accept eg .120 , 仅禁止.0并接受.120

^(0|[1-9]\d*)(\.(?!0$)\d{1,3})?$

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

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