繁体   English   中英

寻找一个REGEXP,数字最多两位小数

[英]Looking for a REGEXP for a number upto two decimal places

我正在尝试这个

/-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/

但这要超过两个小数位,并且它接受值“ .00”

它应该接受以下值
100.00
00.00
0
100
10000000000.00
98173827827.82

它应该拒绝以下值.00
10.098
87.89381938193819
9183983109.9283912
10.aa
adjbdjbdj

我是新来的正则表达式

PS:-我正在尝试以下代码进行JavaScript处理。 因此,请将表达式限制为仅javascript。

提前致谢

这个应该做的:

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

我会做类似的事情:

/-?(?:\d{1,3}(?:,?\d{3})+)?(?:\.\d{1,2})?$/

尝试这个:

if (/^\d+([,.]\d{1,2})?$/im.test(value)) {
        alert("ACCEPTED\n" + value);
    } else {
        alert("REJECTED\n" + value);
    }
}

JSFIDDLE示例

正则表达式说明

^\d+([,.]\d{1,2})?$

Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed, line feed, line separator, paragraph separator) «^»
Match a single character that is a “digit” (ASCII 0–9 only) «\d+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the regex below and capture its match into backreference number 1 «([,.]\d{1,2})?»
   Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match a single character from the list “,.” «[,.]»
   Match a single character that is a “digit” (ASCII 0–9 only) «\d{1,2}»
      Between one and 2 times, as many times as possible, giving back as needed (greedy) «{1,2}»
Assert position at the end of a line (at the end of the string or before a line break character) (line feed, line feed, line separator, paragraph separator) «$»

暂无
暂无

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

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