Regex does not remove digits from string

I have the following line "item number: 237728" I am applying the replacement

str.replace(/[^0-9]/g,'');

      

but the line is not changed and no digits are removed. any idea why? thanks

0


a source to share


2 answers


You assign the return value and use that for example. str = str.replace(/[^0-9]/g,'');

or waiting for the original line to change?



String functions do not modify the original string, they return modified strings.

+10


a source


It worked, try pasting in your browser's location field:

javascript:alert("item number:237728".replace(/[^0-9]/g,""))

      



As Neil says, I suspect your problem might be a string mutability issue. Make sure you grab the return value from replace()

.

+5


a source







All Articles