您好
js检查是否含有非法字符,js 正则过滤特殊字符
//正则function trimTxt(txt){
return txt.replace(/(^\s*)|(\s*$)/g, "")
}
/**
* 检查是否含有非法字符
* @param temp_str
* @returns {Boolean}
*/
function is_forbid(temp_str){
temp_str=trimTxt(temp_str)
temp_str = temp_str.replace('*',"@")
temp_str = temp_str.replace('--',"@")
temp_str = temp_str.replace('/',"@")
temp_str = temp_str.replace('+',"@")
temp_str = temp_str.replace('\'',"@")
temp_str = temp_str.replace('\\',"@")
temp_str = temp_str.replace('$',"@")
temp_str = temp_str.replace('^',"@")
temp_str = temp_str.replace('.',"@")
temp_str = temp_str.replace('',"@")
temp_str = temp_str.replace('<',"@")
temp_str = temp_str.replace('>',"@")
temp_str = temp_str.replace('"',"@")
temp_str = temp_str.replace('=',"@")
temp_str = temp_str.replace('{',"@")
temp_str = temp_str.replace('}',"@")
var forbid_str=new String('@,%,~,&')
var forbid_array=new Array()
forbid_array=forbid_str.split(',')
for(i=0i<forbid_array.lengthi++){
if(temp_str.search(new RegExp(forbid_array[i])) != -1)
return false
}
return true
}
---------------------
作者:dongsir 董先生
来源:董先生的博客
原文链接:js检查是否含有非法字符
版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。转载时请标注:http://dongsir.cn/p/195
js特殊字符转义
点的转义:. ==>\\u002E
美元符号的转义:$ ==>\\u0024
乘方符号的转义:^ ==>\\u005E
左大括号的转义:{ ==>\\u007B
左方括号的转义:[ ==>\\u005B
左圆括号的转义:( ==>\\u0028
竖线的转义:| ==>\\u007C
右方括号转义:] ==>\\u005D
右圆括号的转义:) ==>\\u0029
星号的转义:* ==>\\u002A
加号的转义:+ ==>\\u002B
问号的转义:? ==>\\u003F
反斜杠的转义:\ ==>\\u005C