Regular expression for validating numbers only
Here are the Jasmine tests, so you can see what it does and doesn't handle: the expression will match arbitrary length strings of digits and 123 will return true. Be aware that technically an empty string is a 0-length string of digits, and so it will return true using ^[0-9]*$ If you want to only accept strings containing 1 or more digits, use instead of * As the many others have pointed out, there are more than a few ways to achieve this, but I felt like it was appropriate to point out that the code in the original question only requires a single additional character to work as intended. '[0-9]' // 1-30 decimal digits of integer or fraction.
Lookbehind has limitations, like the phrase cannot include quantifiers. You can use alternation, but only if all alternatives have the same length.
That's why for some cases I'll be using Lookahead instead, which is the same, but in the opposite way. These flavors evaluate lookbehind by first stepping back through the subject string for as many characters as the lookbehind needs, and then attempting the regex inside the lookbehind from left to right.
I'm sure there's a way to change this behavior, but as I said, I've never really done much with regular expressions.
Example of valid values: "3", "-3", "0", "0.0", "1.0", "0.7", "690.7", "0.0001", "-555", "945465464654" Example of not valid values: "a", "", " ", ".", "-", "001", "00.2", "000.5", ".3", "3.", " -1", "--1", "-.1", "-0", "00099", "099" A number can start with a period (without leading digits(s)), and a number can end with a period (without trailing digits(s)).
Search for regular expression for validating numbers only:
To my knowledge, this matches all the variations on numbers that Java and Java Script will ever throw at you, including "-Infinity", "1e-24" and "Na N".