I'm trying to find a regex that checks if a string contains a valid JavaScript number.
So far i got this /(\d|\.\d|e\d)+/i
. It's almost what I need but not completely. It now checks for a digit, a decimal point followed by a digit, or an case-insensitive e followed by a digit. However the regex should also do these things:
- Stop when it reaches e or E for the second time
- Numbers can't start with e or E (a decimal point at the start is okay)
- Stop when it reaches a second decimal point or a decimal point after an e or E
- A sign for negative numbers is only allowed at the start and/or directly after e or E
A few examples:
124e-10.3 => 124e-10
-e-432.4.6 => -432.4
-.955e10e5 => -.955e10
I have no idea now to implement this in a regex... Or is there just a special regex 'token' to check if something is a number? By the way, if I haven't listed something that is also a condition for a JavaScript number, feel free to tell me in the comments or in your answer.