我们需要一个正则表达式,它应该接受最多 2 位小数的有效十进制数字,并可以选择将其括在左右括号中
有效示例: 45.78 99.34 12202.45 (45.22) (65.00) (1255.00)
有人可以帮助我们解决这个问题吗?
最佳答案
解决方案
^(\d+(?:\.\d{1,2})?)$|^(\(\d+(?:\.\d{1,2})?\))$
匹配什么
- 50
- 50.00
- (50)
- (50.00)
说明
^ //Start of string
( // Start capturing group
\d+ // Digit 1 or more times
(?: // Start Non capturing group
\. // Dot
\d{1,2} // Digit 1 to 2 times
)? // End non capturing group and ? means conditional
) // End capturing group
$ //End of string
| //OR (Now we check for numbers enclosed in parenthesis)
^ //Start of string
( // Start capturing group
\( // Match Left Parenthesis
\d+(?:\.\d{1,2})? // Same as above
\) // Match Right Parenthesis
) // End capturing group
$ //End of string
关于javascript - 特定格式的正则表达式(左括号和右括号括起来的有效十进制数字),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30393813/