[英]Javascript Regular expression for basic digits with decimal points?
I am trying to use a regular expression to validate decimal values . 我试图使用正则表达式来验证十进制值。 I wrote below regular expression but it does not allowing first decimal with value like a .5 or .6 or .1
我写下面的正则表达式,但它不允许第一个小数点值为.5或.6或.1
Regular Exp : /^\\d[0-9]{0,13}(\\.\\d{1,2})?$/
常规Exp :/
/^\\d[0-9]{0,13}(\\.\\d{1,2})?$/
Rules : 规则:
Example - Valid inputs 示例 - 有效输入
Example - invalid inputs 示例 - 输入无效
const valid = [ "0", "0.5", "1.55", ".5", "1234567890123", "1234567890123.5", "1234567890123.00", ]; const invalid = [ ".", ".0", "1.234", "5.", "12345678901234", "12345678901234.56", ]; const rgx = /^\\d[0-9]{0,13}(\\.\\d{1,2})?$/ console.log("Checking valid strings (should all be true):"); valid.forEach(str => console.log(rgx.test(str), str)); console.log("\\nChecking invalid strings (should all be false):"); invalid.forEach(str => console.log(rgx.test(str), str));
I believe the following regex should meet all of your criteria: 我相信以下正则表达式应符合您的所有标准:
^(\d{1,13}($|\.\d?\d$)|\.[1-9]\d?$)
first case: 1-13 digits followed either by nothing or by a "." 第一种情况:1-13位数字后跟任何一个或“。” followed by one or two digits
后跟一两位数字
second case: a "." 第二种情况:“。” followed by a non zero digit and at most one other digit
接着是非零数字,最多是另一个数字
const valid = [ "0", "0.5", "1.55", ".5", "1234567890123", "1234567890123.5", "1234567890123.00", ]; const invalid = [ ".", ".0", "1.234", "5.", "12345678901234", "12345678901234.56", ]; const rgx = /^(\\d{1,13}($|\\.\\d?\\d$)|\\.[1-9]\\d?$)/ console.log("Checking valid strings (should all be true):"); valid.forEach(str => console.log(rgx.test(str), str)); console.log("\\nChecking invalid strings (should all be false):"); invalid.forEach(str => console.log(rgx.test(str), str));
i think this should do most of your requirment but not all of them, limit to 9 decimal places 我认为这应该完成你的大部分要求,但不是全部,限制为小数点后9位
( /^(\d+\.?\d{0,9}|\.\d{1,9})$/ )
and this one with no decimal limit 而这一个没有小数限制
( /^(\d+\.?\d*|\.\d+)$/ )
The other answers don't allow for the case of .0x, which I presume is valid? 其他答案不允许.0x的情况,我认为是有效的? I think you need to test for xxx[.xx] and .xx separately ie
我认为你需要分别测试xxx [.xx]和.xx
^(\d{1,13}(\.\d{1,2})?|\.(0[1-9]|[1-9]\d?))$
To match your values you could use a non capturing group with an alternation using |
要匹配你的价值观,你可以使用非捕获组与交替使用
|
and specify what you want to match. 并指定您要匹配的内容。
^(?:\\.[1-9][0-9]?|\\d{1,13}(?:\\.\\d{1,2})?|.\\d{2})$
This will also match .01
and not .0
这也将匹配
.01
而不是.0
Explanation 说明
^
Begin of the string ^
字符串的开头 (?:
Non capturing group (?:
非捕获组
\\.[1-9][0-9]?
Match dot not followed by a 0 and then 0-9 |
Or \\d{1,13}
Match 1 - 13 digits \\d{1,13}
匹配1 - 13位数 (?:
Non capturing group (?:
非捕获组
\\.\\d{1,2}
Match a dot and 1 or 2 digits \\.\\d{1,2}
匹配一个点和1或2位数 )?
Close non capturing group and make it optional |
Or .\\d{2}
Match dot followed by 2 digits .\\d{2}
匹配点后跟2位数 )
Close non capturing group )
关闭非捕获组 $
End of the string $
结束字符串 const valid = [ "0", ".01", "0.5", "1.55", ".5", "1234567890123", "1234567890123.5", "1234567890123.00", ]; const invalid = [ ".", ".0", "1.234", "5.", "12345678901234", "12345678901234.56", ]; const rgx = /^(?:\\.[1-9][0-9]?|\\d{1,13}(?:\\.\\d{1,2})?|.\\d{2})$/; console.log("Checking valid strings (should all be true):"); valid.forEach(str => console.log(rgx.test(str), str)); console.log("\\nChecking invalid strings (should all be false):"); invalid.forEach(str => console.log(rgx.test(str), str));
To not match .01
you could use: 为了不匹配
.01
你可以使用:
^(?:\\d{1,13}(?:\\.\\d{1,2})?|\\.[1-9][0-9]?)$
const valid = [ "0", "0.5", "1.55", ".5", "1234567890123", "1234567890123.5", "1234567890123.00", ]; const invalid = [ ".", ".0", ".01", "1.234", "5.", "12345678901234", "12345678901234.56", ]; const rgx = /^(?:\\d{1,13}(?:\\.\\d{1,2})?|\\.[1-9][0-9]?)$/; console.log("Checking valid strings (should all be true):"); valid.forEach(str => console.log(rgx.test(str), str)); console.log("\\nChecking invalid strings (should all be false):"); invalid.forEach(str => console.log(rgx.test(str), str));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.