Removing Numbers from a String using Javascript

后端 未结 2 774
小蘑菇
小蘑菇 2020-12-05 13:46

How do I remove numbers from a string using Javascript?

I am not very good with regex at all but I think I can use with replace to achieve the above?

It woul

相关标签:
2条回答
  • 2020-12-05 14:14

    \d matches any number, so you want to replace them with an empty string:

    string.replace(/\d+/g, '')
    

    I've used the + modifier here so that it will match all adjacent numbers in one go, and hence require less replacing. The g at the end is a flag which means "global" and it means that it will replace ALL matches it finds, not just the first one.

    0 讨论(0)
  • 2020-12-05 14:28

    Just paste this into your address bar to try it out:

    javascript:alert('abc123def456ghi'.replace(/\d+/g,''))
    

    \d indicates a character in the range 0-9, and the + indicates one or more; so \d+ matches one or more digits. The g is necessary to indicate global matching, as opposed to quitting after the first match (the default behavior).

    0 讨论(0)
提交回复
热议问题