Regex for 1 or 2 digits with one optional decimal place

穿精又带淫゛_ 提交于 2019-12-10 00:33:04

问题


I'm using: http://firstopinion.github.io/formatter.js/index.html to mask inputs.

I'm looking at "patterns" option and am having trouble writing the regex expression for 1 or 2 digits with an optional decimal place.

Good inputs:
2.5
12.5
.5
1

Bad inputs:
.25
123.5
1.55

Thank you for any help!


回答1:


^\d{0,2}(?:\.\d)?$
  • \d{0,2} = 0-2 digits
  • \.\d = decimal point followed by 1 digit
  • (?: ... )? = optional group
  • ^ and $ anchor it to beginning and end

DEMO



来源:https://stackoverflow.com/questions/23705423/regex-for-1-or-2-digits-with-one-optional-decimal-place

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!