原生js正则匹配特殊字符

发现常用demo写在博客上比写在电脑上方便好找的多~~~

var btn = document.getElementById('btn')
console.log(btn)

btn.onclick=function(){
    var val = document.getElementById('ipt').value ;
    console.log(val,'val');
var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]")
    if(pattern.test(val)){
         console.log('ok')
    }
}

猜你喜欢

转载自blog.csdn.net/jiuweiyaoy/article/details/84985424