简体   繁体   English

正则表达式模式只有一个点并匹配整数和十进制数

[英]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. 我当前的正则表达式/^\\d+(\\.\\d{1,2})*$/仅匹配十进制数字。 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+(\.\d{0,2})?$/
  ^             ^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript正则表达式,用于带一个点和两个小数的正数 - JavaScript Regex for positive numbers with one dot and 2 decimal 使用 jquery 验证器插件为仅 2 个十进制数字 + 点 + 逗号设置正则表达式 - Set regex for only 2 decimal numbers + dot + comma with jquery validator plugin 进行仅允许数字、一个点和两个小数位的输入 - Make an input that only allow numbers, one dot and two decimal places Javascript正则表达式-仅匹配一组4个数字 - Javascript Regex - match on only one set of 4 numbers 仅一个点“。”正则表达式 - Only one dot “.” regex javascript中的正则表达式只允许数字和一个点后跟最多2个数字 - regex in javascript allow only numbers and one dot followed by max 2 number 使用正则表达式匹配提取数字、点和逗号 - Extract numbers, dot and comma using regex match 如果字符串包含带数字的整数/小数,则匹配正则表达式 - Regex to match if string contains integer/decimal with digits 如何将正则表达式验证应用于JQuery中的文本框以仅输入小数,整数(如果多个逗号分开) - How to Apply Regex Validation to a textbox in JQuery to enter only decimal(s),integer(s) numbers(if multiple comma seperated) 仅将点与标点匹配的Javascript正则表达式,无数字 - Javascript regex that matches dot as punctuation only, no numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM