I want to extend the following regex to negative numbers.
this.value = this.value.replace(/[^0-9\.]/g, "");
I tried adding minus sign doing something like this, (/[^-0-9.]/g, “”) but this allows minus sign to be entered anywhere in the number. I want to allow only one occurance of the minus sign at the beginning of the number. Later occurances of minus should be ignored.
Please help.
Uh, replacing every non-number character makes that a bit harder – it’s like “negating” the regex. What I came up with is a negative lookahead, preventing a minus – which would be matched by
[^0-9.]– from beeing matched if it is in the beginning of the string (using a start anchor):