Replace multiple characters in one replace call

前端 未结 15 2558
失恋的感觉
失恋的感觉 2020-11-22 17:24

Very simple little question, but I don\'t quite understand how to do it.

I need to replace every instance of \'_\' with a space, and every instance of \'#\' with no

15条回答
  •  感动是毒
    2020-11-22 17:51

    You can also pass a RegExp object to the replace method like

    var regexUnderscore = new RegExp("_", "g"); //indicates global match
    var regexHash = new RegExp("#", "g");
    
    string.replace(regexHash, "").replace(regexUnderscore, " ");
    

    Javascript RegExp

提交回复
热议问题