Javascript-检查字符串是否为有效的CSS编号

问题描述 投票:-2回答:1

我需要RegExp或类似的东西来检测字符串是否为CSS number data type

不得使用外部库或Web API。只是普通的javascript。

有效的CSS编号示例:

12
00012
-12
+12
4.01
-456.8
1.200000
.0
0.0
+0.0
-0.0
-1.2
-.2
+.2
.60
10e3
1.2e2
-1.2e2
.2e2
21.2E5
1.2e+2
-3.4e-2

无效数字:

e2
1e
20.
20.e3
1.2.
+-12.2
0x11
0b11
0o11
-.
+.
.
javascript css numbers
1个回答
1
投票

我的解决方法是:

function isCssNumber(string) {
    return /^[-+]{0,1}\d*\.{0,1}\d+([eE]{0,1}[-+]{0,1}\d+){0,1}$/.test(string)
}

© www.soinside.com 2019 - 2024. All rights reserved.