I need to remove all non numeric characters except the dash. This is my attempt (based on: Regex to get all alpha numeric fields except for comma, dash and single quote):
var stripped = mystring.replace(/[-0-9]+/g, '');
But that doesn't work :-(
I'd suggest:
var stripped = string.replace(/[^0-9\-]/g,'');
JS Fiddle demo.
^
in the character class (within the [
and ]
) is the NOT operator, so it matches characters which are not 0-9
or the (escaped) -
character.
As noted in the comment to this answer, by Ted Hopp, it's not necessary to escape the -
when it's the last character, but I habitually do so in order to save having to remember that proviso.
References:
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