You must add an option. global
var foo = "bar01";
alert(foo.replace(/\d/g,''));
Itβs clear that 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 speed difference will be very very small if you do not parse the megabytes of text)
If you want to test http://jsperf.com/replace-digits , the second one is apparently faster for blob of 10 digits and large texts.
source
share