All I want is to strip all the numbers from a string.
So
var foo = "bar01";
alert(foo.replace(/\d/,''));
Which obviously gives "bar1" because I've only specified one digit. So why doesn't this work:
var foo = "bar01";
alert(foo.replace(/\d*/,''));
Which gives "bar01"
You must add the global
option
var foo = "bar01";
alert(foo.replace(/\d/g,''));
Clearly you can even do something like
var foo = "bar01";
alert(foo.replace(/\d+/g,''));
but I don't know if it will be faster (and in the end the difference of speed would be very very very small unless you are parsing megabytes of text)
If you want to test http://jsperf.com/replace-digits the second one seems to be faster for "blobs" of 10 digits and big texts.
You probably want to specify the g
flag: foo.replace(/\d/g,'')
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