For example:
10
0.1
1.23234
123.123
0.000001
1.000
.3
And wrong examples:
0001.2
-12
-1.01
+2.3
EDIT: standart JavaScript regex.
[0-9]+|[0-9]+). This regular expression matches an optional sign, that is either followed by zero or more digits followed by a dot and one or more digits (a floating point number with optional integer part), or that is followed by one or more digits (an integer).
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
A question mark ( ? ) immediately following a character means match zero or one instance of this character . This means that the regex Great!? will match Great or Great! .
Try this here
^(?:[1-9]\d*|0)?(?:\.\d+)?$
See it here online on Regexr
If matching the empty string is not wanted, then you can add a length check to your regex like
^(?=.+)(?:[1-9]\d*|0)?(?:\.\d+)?$
The positive lookahead (?=.+)
ensures that there is at least 1 character
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With