例如:
10
0.1
1.23234
123.123
0.000001
1.000
.3
错误的例子:
0001.2
-12
-1.01
+2.3
编辑:标准JavaScript正则表达式.
ste*_*ema 33
试试吧
^(?:[1-9]\d*|0)?(?:\.\d+)?$
Run Code Online (Sandbox Code Playgroud)
看到它在网上的Regexr
如果不需要匹配空字符串,那么您可以在正则表达式中添加长度检查
^(?=.+)(?:[1-9]\d*|0)?(?:\.\d+)?$
Run Code Online (Sandbox Code Playgroud)
积极的向前看(?=.+)确保至少有1个字符
这将通过所有测试用例,启用多线模式:
/^(?!0\d)\d*(\.\d+)?$/mg
Run Code Online (Sandbox Code Playgroud)
说明:
/^ # start of regex and match start of line
(?!0\d) # not any number with leading zeros
\d* # consume and match optional digits
(\.\d+)? # followed by a decimal and some digits after, optional.
$ # match end of line
/mg # end of regex, match multi-line, global match
Run Code Online (Sandbox Code Playgroud)
RegExr: http://regexr.com?2tpd0
考虑正则表达式:
^[0-9]*(?:\.[0-9]*)?$
Run Code Online (Sandbox Code Playgroud)
此正则表达式将匹配浮点数,例如:
- .343
- 0.0
- 1.2
- 44
- 44.
- 445.55
- 56.
- . //Only dot(.) also matches
- empty string also matches
Run Code Online (Sandbox Code Playgroud)
上面的正则表达式不会接受:
- h32.55 //Since ^ is used. So, the match must start at the beginning
of the string or line.
- 23.64h //Since $ is used. So, the match must occur at the end of the string or before \n at the end of the line or string.
Run Code Online (Sandbox Code Playgroud)
考虑正则表达式:
^[0-9]+(?:\.[0-9]+)?$
Run Code Online (Sandbox Code Playgroud)
此正则表达式将匹配浮点数,例如:
- 45
- 45.5
- 0.0
- 1.2
- 445.55
Run Code Online (Sandbox Code Playgroud)
此正则表达式将不接受:
- h32.55 //Since ^ is used. So, the match must start at the beginning
of the string or line.
- 23.64h //Since $ is used. So, the match must occur at the end of the string or before \n at the end of the line or string.
- 44.
- . //Only dot(.) does not matches here
- empty string also does not matches here
Run Code Online (Sandbox Code Playgroud)
纯浮点:
^(([0-9]+(?:\.[0-9]+)?)|([0-9]*(?:\.[0-9]+)?))$
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28845 次 |
| 最近记录: |