如何匹配javascript表达式中所有可能的数字?

时间:2011-08-02 22:59:14

标签: javascript regex

我看过很多正则表达式的匹配数字,但没有一个能完全捕获所有有效数字。

例如,我需要匹配以下所有内容:

//All of these can be preceded/followed by any of: +=-()!~%$#^&*{}[]|\;<>,
23
-23
4.8
1.3e-8
1.38e+5
-1.3e-2
-1.4e+2

但不符合以下条件:

w23
-23w
_4.8 //This would see 8 as a number but not _4.
4yes
1.3ed-8 //This would see 1 and -8 as numbers but not .3ed
12dog
12foo

有没有办法做这样的正则表达式?

2 个答案:

答案 0 :(得分:5)

我觉得你的要求有点奇怪,但是这个正则表达式可以满足你所有的例子:

-?\b\d+(\.\d+)?([eE][-+]?\d+)?\b

使其符合您的规范的关键是使用\b字边界匹配。

每个结果:

23:
  23
-23:
  -23
4.8:
  4.8
1.3e-8:
  1.3e-8
1.38e+5:
  1.38e+5
-1.3e-2:
  -1.3e-2
-1.4e+2:
  -1.4e+2
w23:
-23w:
_4.8:
  8
4yes:
1.3ed-8:
  1
  -8
12dog:
12foo:

答案 1 :(得分:2)

如果您使用以下正则表达式m标记(^$匹配每个新行的开头和结尾),它将返回对您提供的所有有效内容有效数字,对提供的无效数字无效:

^[+=\-()!~%$#\^&*{}[\]|\\;<>,]*\d+[\.\d]*([a-zA-Z][\+\-]\d+)?[+=\-()!~%$#\^&*{}[\]|\\;<>,]*$

细分:

^                                   #start of string (line, with "m" flag)
  [+=\-()!~%$#\^&*{}[\]|\\;<>,]*    #zero or more of any of these characters.
  \d+                               #one or more digits (basic number: 123)
  [\.\d]*                           #zero or more periods or digits (to account for floats: 1.11)
  ([a-zA-Z][\+\-]\d+)?              #one or zero instance of a letter, a + or - sign, then 1 or more digits (to account for scientific notation floats: 1.23e+3 or 1.23e-3)
  [+=\-()!~%$#\^&*{}[\]|\\;<>,]*    #once again, zero or more of any of these characters.
$                                   #end of string (line, with "m" flag)