『壹』 js中filter过滤用法总结
filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。
注意: filter() 不会对空数组进行检纳卖测。
注意: filter() 不会改变原始数组。
| 参数 | 描述 |
| function(currentValue, index,arr) | 必须。函数,数组中的每个元素都会执行这个函数
函数参数:
| 参数 | 描述 |
| currentValue | 必须。当前元素的值 |
| index | 可选。当期元素的索引值 |
| arr | 可选。当期元素属于的数组对象 |
|
| thisValue | 可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。
如果省略了 thisValue ,"this" 的值为 "undefined" |
首先回顾一下filter的作用:过滤数组中符合条件的元素
另外也可以用来过滤对象数组中老衡符合条件的对象,eg:
|
目前比较常用的方法是使用ES6的set完成,eg:
结合map使用可以先过滤出符合条件的对象然后去除某些不需洞含逗要的字段,比如:
简单讲filter就是一个数组过滤器,参数接收一个函数,数组的每一项经过函数过滤,返回一个符合过滤条件的新数组
函数接收三个参数:
『贰』 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
『叁』 js 如何过滤div里内的指定字符
String.replace(正则表达式,"")
replace是string类型内置的替换方法,第一个参数可以是正则表达式,第二个参数是版想要权替换成的文本,正则中可以使用/g来表示替换所有匹配的文本,不使用则代表只替换匹配到的第一个字符对象,将第二个参数设为空字符串便可达到过滤的效果。
具体正则需要你自己去了解关于正则的知识了,祝你好运。
『肆』 用js过滤掉 用户输入到文本框里面的脏话,求代码
最笨的方法是:
varstr='zz112kdd';//输入抄的内容
vararr={'1':'1','2':'2','3':'3'};//要过滤掉的字符,用对象方便查找,如果是数组的话就要遍历
varresult='';//过滤后字符串
for(vari=1,length=str.length;i<length;i++){
vars=str.substring(i-1,i);//截取字符
vars1=arr[s];//判断这个字符是否在敏感词对象里,匹配不到就返回undefined
if(s1==undefined)result+=s;//不是敏感词就保留下来
}
估计正则表达式也可以实现,但稍微有点麻烦。
『伍』 js 怎么屏蔽文本框里的特殊字符 如@#¥%等
用js正则表达式呀。
var value=document.getElementById('typeTest').value;
var regExp = /[a-zA-Z]+/g
if(regExp.test(value)){
alert('正确');
}
具体的去查查 js 正则 验证
『陆』 js 过滤所有空字符串
首先要理解这个方法replace。
语法:string.replace(searchvalue,newvalue)
searchvalue:必须。规定子字符串或要替换的版模式的 RegExp 对象。
请注意权,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象。
newvalue:必需。一个字符串值。规定了替换文本或生成替换文本的函数。
注意RegExp 对象,而// 就是一个RegExp 对象(正则表达式)。
//varRegExp=newRegExp(pattern,attributes);
varreg=newRegExp('','g');
str=str.replace(reg,'');//跟str=str.replace(//g,'')是一样意思。
而这里的g是global的缩写,意思是全局匹配;
如果没有加g,那么只是匹配第一个就结束了,对应str2,否则就全局匹配,对应str1
『柒』 jsp中如何过滤非法字符
可以利用javascript验证正则表达式的方式对非法字符做处理,同样Java也可以利用正则表达式帮你过滤非法字符,最终还得更具你的具体需求来定。
『捌』 如何用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;
}