❶ Js字符串的正则匹配 如何过滤掉指定特征的字符串
String.replace(正则表达式,"")
replace是string类型内置的替换方法,第一个参数可以是正则表达式,第二个版参数是想权要替换成的文本,正则中可以使用/g来表示替换所有匹配的文本,不使用则代表只替换匹配到的第一个字符对象,将第二个参数设为空字符串便可达到过滤的效果。
具体正则需要你自己去了解关于正则的知识了,祝你好运。
❷ JS过滤字符
可以使用字符串的match方法匹配出你想要的内容,match支持传入一个正则表达式。
根据你的需求版,只需要权写一个匹配数字的正则即可,比如var matches="__20__40__43__on__".match(/\d+/g);
得到的matches数组即为符合你要求的内容,不需要split拆分字符串也不需要循环处理,只需要这样简单的一个方法调用即可
参考match方法:http://www.w3school.com.cn/jsref/jsref_match.asp
❸ JS如何去除指定字符串
两种方式可以实现
1:使用replace函数替换
var str="hello world!";
str=str.replace("l","");
即使用空串替换某一个字符串,则专是可以实现去除指定字符串功能属
2:使用字符串分割函数在聚合
var str="hello world!"
var items=str.split("o")
会得到一个数组,数组中包括利用o分割后的多个字符串(不包括o)
var newStr=items.join("");
会得到一个新字符串,将数组中的数组使用空串连接成一个新字符串
❹ js中用正则表达式 过滤特殊字符 校验所有输入域是否含有特殊符号
function stripscript(s) {
var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]")
var rs = "";
for (var i = 0; i < s.length; i++) {
rs = rs + s.substr(i, 1).replace(pattern, '');
}
return rs;
}
❺ javascript去掉符号
<script>
var str=" ,,b!@#$4%asd^df,g&*(1士大夫";
var reg=/[^0-9,]*/g;
var s=str.replace(reg,"");
alert( s);
</script>
-----------------------------------------
为了更加明白:
<script>
var str=" ,,b!@#$4%asd^df,g&*(1士大夫";
var reg=/[^0-9,]*/g;
var s=str.replace(reg,"");
alert("过滤前的字符串:"+str+"\n过滤后的字符串:"+s);
</script>
------------------------------------
我本地测试没有问题!
把下面代码保存成html文件自己测试一下吧
<textarea name=a rows=10 cols=50> ,,b!@#$4%a\rsd^d\ff,g&*(1士大夫\nwerwer,wer4</textarea><button onclick="c()">CiclkMe</button><textarea name=b rows=10 cols=50>显示结果</textarea>
<script>
function c(){
var str=" ,,b!@#$4%a\rsd^d\ff,g&*(1士大夫\nwerwer,wer4";
var str2=a.innerText;
var reg=/[^0-9,]*/g;
var s=str2.replace(reg,"");
alert("过滤前的字符串:"+str2+"\n过滤后的字符串:"+s);
b.innerText=s;
}
</script>
❻ 求js去除字符串中所有 和&等特殊符号。
vara="今天是星期五,明天又可以放假了&好好休|息一下"
varb=a.replace(/[&|\*^%$#@-]/g,"");
alert(b);
需要去掉什么符号,就在正则表达式中加上什么符号
❼ JS字符过滤
用正则[^1-4]
❽ JS去掉指定字符串
array=**.split(",")分割,去掉array[0],和array[array.length-1],在用IF判断里面不是1812的都连接起来,
var i;
var str;
var j=0;
for(i=1;i<array.length-1;i++)
{
if(array[i]=="1812"){
}else
{
if(j==1){
str=str+",";
}
str=str+array[i];
j=1;
}
}
比如按照你给出的应该为
str=array[1]+","+array[3]+","+array[4]
array[2]=1812 所以跳过
❾ 如何用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;
}
❿ 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