2

So I need a RegExp in Javascript to check for any number:

2 -> true
3.14 -> true
2. -> true
.2 -> true
0 -> true

And it should also work for these:

Infinity -> true
-Infinity -> true
-0 -> true
-22 -> true
2e-3 -> true
5e-14 -> true
3e+2 -> true
8e2 -> true

The d will only allow positive integers, which is far from what i need to my RegExp, and I am not good at all with RegExps, so what's the most efficient RegExp to do this?

1 Answers1

2

You can use this regex:

[-+]?(?:(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?|Infinity)

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643