jQuery only allow numbers,letters and hyphens

前端 未结 3 1681
执笔经年
执笔经年 2021-01-16 08:25

How can I remove everything but numbers,letters and hyphens from a string with jQuery?

I found this code which allows only alphanumerical characters only but I\'m no

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-16 09:02

    Since there is so much attention on including a hyphen within a character class amongst these answers and since I couldn't find this information readily by Googling, I thought I'd add that the hyphen doesn't need to be escaped if it's the first character in the class specification. As a result, the following character class will work as well to specify the hyphen in addition to the other characters:

    [-a-zA-Z0-9]
    

提交回复
热议问题