Ⅰ JAVA特殊字符过滤方法
public static String StringFilter(String str) throws PatternSyntaxException {
// 只允许字母和数字
// String regEx = "[^a-zA-Z0-9]";
// 清除掉所有特殊字符
String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.replaceAll("").trim();
}
Ⅱ 如何用js或则jquery过滤特殊字符
1、jQuery使用正则匹配替换特殊字符
functionRegeMatch(){
varpattern=newRegExp("[~'!@#$%^&*()-+_=:]");
if($("#name").val()!=""&&$("#name").val()!=null){
if(pattern.test($("#name").val())){
alert("非法字符!");
$("#name").attr("value","");
$("#name").focus();
returnfalse;
}
}
}
2、jQuery限制输入ASCII值
//数字0-9的ascii为48-57
//大写A-Z的ascii为65-90
//小写a-z的ascii为97-122
//----------------------------------------------------------------------
//<summary>
//限制只能输入数字和字母
//</summary>
//----------------------------------------------------------------------
$.fn.onlyNumAlpha=function(){
$(this).keypress(function(event){
vareventObj=event||e;
varkeyCode=eventObj.keyCode||eventObj.which;
if((keyCode>=48&&keyCode<=57)||(keyCode>=65&&keyCode<=90)||(keyCode>=97&&keyCode<=122))
returntrue;
else
returnfalse;
}).focus(function(){
this.style.imeMode='disabled';
}).bind("paste",function(){
varclipboard=window.clipboardData.getData("Text");
if(/^(d|[a-zA-Z])+$/.test(clipboard))
returntrue;
else
returnfalse;
});
};
//-----调用方法$("#文本框id").onlyNumAlpha();
3、js正则匹配过滤
functionstripscript(s)
{
varpattern=newRegExp("[`~!@#$^&*()=|{}':;',\[\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]")
varrs="";
for(vari=0;i<s.length;i++){
rs=rs+s.substr(i,1).replace(pattern,'');
}
returnrs;
}
Ⅲ php获取微信昵称时如何过滤特殊字符
用个函数过滤掉特殊字符
/** * $str 微信昵称 **/ public function filter($str) { if($str){ $name = $str; $name = preg_replace('/\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]/', '', $name); $name = preg_replace('/xE0[x80-x9F][x80-xBF]‘.‘|xED[xA0-xBF][x80-xBF]/S','?', $name); $return = json_decode(preg_replace("#(\\\ud[0-9a-f]{3})#ie","",json_encode($name))); if(!$return){ return $this->jsonName($return); } }else{ $return = ''; } return $return; }
Ⅳ 如何过滤MAP中的特殊字符
//用来防页面来上输入的源带有破坏性内容
Map<String, String> map = new HashMap<String, String>();
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");
for(Iterator iterator = dataMap.entrySet().iterator(); iterator.hasNext();){
Map.Entry m = (Entry) iterator.next();
String mapValue = (String) m.getValue();
if("".equals(mapValue)||mapValue!=null){
String result = "";
//将取出的值进行相关操作然后再塞到Map里
......
m.setValue(result);
}
}
Ⅳ 如何过滤特殊字符和乱码的字符
这是编来码引起的,把数据库表源的那个字段编码改成utf-8格式 alter table user(表名) CHANGE old(老字段) new(新字段) varchar(100) charset utf8 后面的语句的编码就是utf8,不要改成utf-8,MySQL不识别,不用改字段名称就直接都写原来的字段名。
Ⅵ js 正则过滤特殊字符
您好
js检查是否含有非法字符,js 正则过滤特殊字符
//正则
functiontrimTxt(txt){
returntxt.replace(/(^s*)|(s*$)/g,"");
}
/**
*检查是否含有非法字符
*@paramtemp_str
*@returns{Boolean}
*/
functionis_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('}',"@");
varforbid_str=newString('@,%,~,&');
varforbid_array=newArray();
forbid_array=forbid_str.split(',');
for(i=0;i<forbid_array.length;i++){
if(temp_str.search(newRegExp(forbid_array[i]))!=-1)
returnfalse;
}
returntrue;
}
---------------------
作者:dongsir 董先生
来源:董先生的博客
原文链接:js检查是否含有非法字符
版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。转载时请标注:http://dongsir.cn/p/195
Ⅶ jQuery 过滤html标签属性的特殊字符
您好,如果在表单中需要提交一字符串,其中包含,< > " &字符时,当我们把这字符串显示到jsp页面时,会和html标签产生冲突,导致web页面的某些部分消失或者格式不正确。为了解决以上问题,需要在显示之前,对字符串进行代码过滤。
把字符串中的 < 替换为 &It;
> 替换为 >
" 替换为 "
& 替换为 &
这里给出一个静态的过滤代码,供大家参考:
public class StringUtils {
/**
* This method takes a string which may contain HTML tags (ie, <b>,
* <table>, etc) and converts the '<'' and '>' characters to their HTML escape sequences.
* @param input the text to be converted.
* @return the input string with the characters '<' and '>' replaced with their HTML escape sequences.
*/
public static final String escapeHTMLTags(String input) {
//Check if the string is null or zero length -- if so, return
//what was sent in.
if (input == null || input.length() == 0) {
return input;
}
//Use a StringBuffer in lieu of String concatenation -- it is
//much more efficient this way.
StringBuffer buf = new StringBuffer(input.length());
char ch = ' ';
for (int i = 0; i < input.length(); i++) {
ch = input.charAt(i);
if (ch == '<') {
buf.append("<");
}
else if (ch == '>') {
buf.append(">");
}else if(ch == '"'){
buf.append(""");
}else if(ch == '&'){
buf.append("&");
}
else {
buf.append(ch);
}
}
return buf.toString();
}
}
此时,只需在jsp中对字符串调用此方法(StringUtils.escapeHTMLTags(str))即可。
Ⅷ java正则表达式过滤特殊字符
Stringregexp="[^'"%]*";
Stringstring="abc%";
System.out.println(string.matches(regexp));
Ⅸ 求一个正则表达式过滤特殊字符但是不过滤@下划线之类的
这个的话也可以通过正则限定允许输入的字符来实现
比如说:字母开头6-15位字符就可以写成如下方式
/^[a-zA-Z][a-zA-Z0-9]{5,15}$/
这样也可以达到过滤特殊字符的效果