简体   繁体   中英

Regex pattern to have only one dot and match integer and decimal numbers

I'm struggling with creating proper regex pattern to match such strings:

"3" // true
"3." // true
"3.1" // true
"3.22" // true

And such strings should fail matching:

"3.." // false
"3.222" // false

My current regex /^\\d+(\\.\\d{1,2})*$/ matches only decimal numbers. I've tried several updates to it but cannot accept all rules.

Make the decimal part optional + you forgot to put \\ before first d , and remove * from the decimal part.

/^\d+(\.\d{0,2})?$/
  ^             ^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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