正则表达式,十进制数字必须是特定数字

时间:2018-11-01 22:04:23

标签: javascript regex

我需要一个正则表达式来表示数字

+2.25
-9,75
+02,50
-10.00

到目前为止,我要做的是这个 ^([-+] \ d {2} [。,] [00 | 25 | 50 | 75] {1,2})$ < / p>

1- [-+] = obligatory at the beginning
2- \d{2} = any number between 0 and 99
3- [.,] = separator can be .or,
4- [00|25|50|75]{1,2} = input must be 00 or 25 or 50 or 75

数字4无效,您可以在https://regex101.com/处进行测试。

我想要和不想得到的结果

-9.75 Good
-9.77 Bad

结尾必须始终为 00 25 50 75

1 个答案:

答案 0 :(得分:2)

您需要先接受1或2个数字。

^[-+]\d{1,2}[.,](00|25|50|75)$

对正则表达式的唯一修改:\ d {1,2},它接受​​一位或两位数。

另一个选择:

^[-+]\d?\d[.,](00|25|50|75)$

\ d?\ d使第一个数字为可选。

您可以herehere

对其进行测试