jQuery remove special characters from string and more

后端 未结 7 1719
北荒
北荒 2020-12-12 17:58

I have a string like this:

var str = \"I\'m a very^ we!rd* Str!ng.\";

What I would like to do is removing all special characters from the a

7条回答
  •  半阙折子戏
    2020-12-12 18:25

    replace(/[^a-z0-9\s]/gi, '') will filter the string down to just alphanumeric values and replace(/[_\s]/g, '-') will replace underscores and spaces with hyphens:

    str.replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, '-')
    

    Source for Regex: RegEx for Javascript to allow only alphanumeric

    Here is a demo: http://jsfiddle.net/vNfrk/

提交回复
热议问题