正浮点数的正则表达式 [英] Regex for positive float numbers
本文介绍了正浮点数的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:
10
0.1
1.23234
123.123
0.000001
1.000
.3
For example:
10
0.1
1.23234
123.123
0.000001
1.000
.3
错误示例:
0001.2
-12
-1.01
+2.3
And wrong examples:
0001.2
-12
-1.01
+2.3
标准 JavaScript 正则表达式.
standart JavaScript regex.
推荐答案
在这里试试
^(?:[1-9]\d*|0)?(?:\.\d+)?$
如果不想要匹配空字符串,那么您可以向正则表达式添加长度检查
If matching the empty string is not wanted, then you can add a length check to your regex like
^(?=.+)(?:[1-9]\d*|0)?(?:\.\d+)?$
正向前瞻 (?=.+)
确保至少有 1 个字符
The positive lookahead (?=.+)
ensures that there is at least 1 character
这篇关于正浮点数的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文