Javascript string replace with regex to strip off illegal characters

前端 未结 4 1292
挽巷
挽巷 2020-12-14 05:11

Need a function to strip off a set of illegal character in javascript: |&;$%@\"<>()+,

This is a classic problem to be solved with regexes, whi

4条回答
  •  不知归路
    2020-12-14 06:11

    You need to wrap them all in a character class. The current version means replace this sequence of characters with an empty string. When wrapped in square brackets it means replace any of these characters with an empty string.

    var cleanString = dirtyString.replace(/[\|&;\$%@"<>\(\)\+,]/g, "");
    

提交回复
热议问题