RegEx needed to match number to exactly two decimal places

后端 未结 5 1385
醉梦人生
醉梦人生 2020-12-06 09:48

I need some regex that will match only numbers that are decimal to two places. For example:

  • 123 = No match
  • 12.123 = No match
  • 12.34 = Match
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 10:45

    You can also try Regular Expression

    ^\d+(\.\d{1,2})?$
    
    or
    var regexp = /^\d+\.\d{0,2}$/;
    
    // returns true
    regexp.test('10.5')
    
    or
    [0-9]{2}.[0-9]{2}
    
    or
    ^[0-9]\d{0,9}(\.\d{1,3})?%?$
    
    or
    ^\d{1,3}(\.\d{0,2})?$
    

提交回复
热议问题