Removing invalid characters in JavaScript

后端 未结 3 856
一个人的身影
一个人的身影 2020-12-29 05:30

Can someone provide a regular expression to search and replace illegal characters found

Example, removing �

I am not sure how many types of \'illegal\' char

3条回答
  •  灰色年华
    2020-12-29 05:57

    Instead of having a blacklist, you could use a whitelist. e.g. If you want to only accept letters, numbers, space, and a few punctuation characters, you could do

    myString.replace(/[^a-z0-9 ,.?!]/ig, '')
    

提交回复
热议问题