Numeric characters are
0 1 2 3 4 5 6 7 8 9
dot
comma
minus at the start of the string
I need to remove all non numeric characters from a string. Here examples
123 -> 123
123.45 -> 123.45
123,45 -> 123,45
-123 -> -123
-123.45 -> -123.45
-123,45 -> -123,45
--123.45 -> -123.45
--123,45 -> -123,45
a -> empty string
-a -> -
a123 -> 123
-a123 -> -123
a123.45 -> 123.45
-a123.45 -> -123.45
a123.b45 -> 123.45
-a123.b45 -> -123.45
123.-34 -> 123.34
-123.-34 -> -123.34
--123.-34 -> -123.34
Here is what I have so far
"123.45abc".replace(/[^0-9.,]/g,""); -> 123.45
but this fails
"-123.45abc".replace(/[^0-9.,]/g,""); -> 123.45
This works
"-123.45abc".replace(/-[^0-9.,]/g,""); -> -123.45
but now this fails
"-123.45-abc".replace(/-[^0-9.,]/g,""); -> -123.45bc
This answer almost solves my problem but minus signs inside the string are not removed.
I am doing this in javascript using regex. Regex is not compulsory.
Update
The input will have at most one comma or one dot. 12.34.56.78 will never be input