正则 去除字符串中的特殊字符

var str="你的字符串~!#$%^&*()_+";

//你要清除的符号都放在方括号里,记得要用斜杠\转义一下,不然会出错

str = str.replace(/[要清除的符号]/g,'');

eg:
var url = 'https://gw.dushu.io/r/34440a483c1c'
console.log(trim(url))   //结果:httpsgwdushuior34440a483c1c
function trim(url) {
    url = url.replace(/\:/g,'');
    url = url.replace(/\//g,'');
    url = url.replace(/\./g,'');
    return url
}

 

posted @ 2018-08-09 10:53  雾以泪聚rx  阅读(14762)  评论(0编辑  收藏  举报