且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用JavaScript从字符串中删除除空格之外的所有特殊字符

更新时间:2023-01-22 19:20:52

您应该使用单个正则表达式的字符串替换功能。
假设特殊字符,你的意思是任何不是字母,这是一个解决方案:

You should use the string replace function, with a single regex. Assuming by special characters, you mean anything that's not letter, here is a solution:

var str = "abc's test#s";
alert(str.replace(/[^a-zA-Z ]/g, ""));