Javascript string replace with regex to strip off illegal characters

前端 未结 4 1291
挽巷
挽巷 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条回答
  •  旧时难觅i
    2020-12-14 05:57

    What you need are character classes. In that, you've only to worry about the ], \ and - characters (and ^ if you're placing it straight after the beginning of the character class "[" ).

    Syntax: [characters] where characters is a list with characters.

    Example:

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

提交回复
热议问题