Kosmetika
Kosmetika

Reputation: 21304

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.

Upvotes: 1

Views: 3847

Answers (1)

pavel
pavel

Reputation: 27082

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

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

Upvotes: 3

Related Questions