I have the following string "item number:237728" I'm applying replace
str.replace(/[^0-9]/g,'');
but the string does not change, non digits are not removed. any idea why? thanks
I have the following string "item number:237728" I'm applying replace
str.replace(/[^0-9]/g,'');
but the string does not change, non digits are not removed. any idea why? thanks
This worked, try pasting into the location box of your browser:
javascript:alert("item number:237728".replace(/[^0-9]/g,""))
As Neal says, I suspect your problem might be that of string mutability. Make sure you catch the return value from replace()
.
Are you assigning the returned value and using that, e.g. str = str.replace(/[^0-9]/g,'');
, or expecting the original string to change?
String functions don't modify the original string, they return the modified strings.