Javascript 正在查找小数点后两位以内的数字的REGEXP
我正在尝试这个 /-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/ /-?(?:\d+\d{1,3}(?:,\d{3})+)?(?:\.\d+)$/ 但这需要两位以上的小数,而且它接受的值是“.00” 它应该接受以下值Javascript 正在查找小数点后两位以内的数字的REGEXP,javascript,jquery,regex,pattern-matching,Javascript,Jquery,Regex,Pattern Matching,我正在尝试这个 /-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/ /-?(?:\d+\d{1,3}(?:,\d{3})+)?(?:\.\d+)$/ 但这需要两位以上的小数,而且它接受的值是“.00” 它应该接受以下值 100.00 00.00 0 100 1000000000.00 98173827827.82 它应该拒绝以下值 .00 10.098 87.89381938193819 9183983109.9283912 10.aa adjbdjbdj 我
100.00
00.00
0
100
1000000000.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);
}
}
正则表达式解释
^\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) «$»
用于测试和可视化此正则表达式的功能-