How to remove numbers from a string?

前端 未结 7 1678
遇见更好的自我
遇见更好的自我 2020-12-12 19:56

I want to remove numbers from a string:

questionText = \"1 ding ?\"

I want to replace the number 1 number and the question mar

7条回答
  •  温柔的废话
    2020-12-12 20:51

    Very close, try:

    questionText = questionText.replace(/[0-9]/g, '');
    

    replace doesn't work on the existing string, it returns a new one. If you want to use it, you need to keep it!
    Similarly, you can use a new variable:

    var withNoDigits = questionText.replace(/[0-9]/g, '');
    

    One last trick to remove whole blocks of digits at once, but that one may go too far:

    questionText = questionText.replace(/\d+/g, '');
    

提交回复
热议问题