I came to scenario where I only want [0-9 or .] For that I used this regex:
[0-9.]$
This regex accepts 0-9 and . (dot for decimal). But when I write something like this
1,1
It also accepts comma (,). How can I avoid this?
I came to scenario where I only want [0-9 or .] For that I used this regex:
[0-9.]$
This regex accepts 0-9 and . (dot for decimal). But when I write something like this
1,1
It also accepts comma (,). How can I avoid this?
Once you are looking into a way to parse numbers (you said dot is for decimals), maybe you don't want your string to start with dot neither ending with it, and must accept only one dot. If this is your case, try using:
^(\d+\.?\d+|\d)$
where:
\d+
stands for any digit (one or more)\.?
stands for zero or one of literal dot\d
stands for any digit (just one)You can see it working here
Or maybe you'd like to accept strings starting with a dot, which is normally accepted being 0
as integer part, in this case you can use ^\d*\.?\d+$
.
This regex [0-9.]$
consists of a character class that matches a digit or a dot at the end of a line $
.
If you only want to match a digit or a dot you could add ^
to assert the position at the start of a line:
If you want to match one or more digits, a dot and one or more digits you could use:
This regex may help you:
/[0-9.]+/g
Accepts 0 to 9 digits and dot(.).
Explanation:
Match a single character present in the list below [0-9.]+
+
Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
0-9
a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
.
matches the character . literally (case sensitive)
You can test it here