开发者

Reg-Exp for 0,0.0 or 100.20 or 0.99 or 9999.99

开发者 https://www.devze.com 2023-03-12 07:15 出处:网络
I want to be able to allow 0 or 0.0 or 100.20 or 1000 or 0.99 I开发者_运维问答 want to enter 4 digit+decimal point+2 digit

I want to be able to allow 0 or 0.0 or 100.20 or 1000 or 0.99

I开发者_运维问答 want to enter 4 digit+decimal point+2 digit

Where decimal & 2 digit are not mandatory & out of 4 digits I want to allow 1 digit too

I want the exact regex to match this.


/\d{1,4}(\.(\d{1,2}))?/

This says "one to four digits optionally followed by a . and one or two digits".

Surround that with \b if you want to match (and extract in the following) a word within a string:

/\b(\d{1,4}(\.(\d{1,2}))?)\b/

If you want the string to match strictly that pattern and nothing else (i.e. nothing before or after), you can anchor it with ^ and $:

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

精彩评论

暂无评论...
验证码 换一张
取 消