views:

422

answers:

2

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

+4  A: 

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().

unwind
Clever way to test it!
DOK
+7  A: 

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.

Neal Maloney
that's right. I think the op wants str = str.replace(/[^0-9]/g,'');
Luke Schafer