Regular expression for validating numbers only
15-Mar-2020 03:20
I haven't used regular expressions at all, so I'm having difficulty troubleshooting.
I want the regex to match only when the contained string is all numbers; but with the two examples below it is matching a string that contains all numbers plus an equals sign like "1234=4321".
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.
A digit in the range 1-9 followed by zero or more other digits then optionally followed by a decimal point followed by at least 1 digit: i.e.