导航:首页 > 净水问答 > js过滤字符串符号

js过滤字符串符号

发布时间:2021-01-03 16:55:03

① js 如何过滤div里内的指定字符

String.replace(正则表达式,"")
replace是string类型内置的替换方法,第一个参数可以是正则表达式,第二个参数是版想要权替换成的文本,正则中可以使用/g来表示替换所有匹配的文本,不使用则代表只替换匹配到的第一个字符对象,将第二个参数设为空字符串便可达到过滤的效果。
具体正则需要你自己去了解关于正则的知识了,祝你好运。

② 求js去除字符串中所有  和&等特殊符号。

vara="今天是星期五,明天又可以放假了&好好休|息一下"
varb=a.replace(/[&|\*^%$#@-]/g,"");
alert(b);

需要去掉什么符号,就在正则表达式中加上什么符号

③ js 的字符串可用什么符号去代替某一串

^function delImgTag(str){
return str.replace(/<img[^>]*>/ig,'');
}

使用方法如下:
myStr=“sjdkfkflek <img asdsdf > dfdsdfff <img fdfsdefff> fsdgsdgdgsg <img dfsgfsf>”;
var result = delImgTag(myStr);//去掉img标签回
alert(result);//弹出结答果

④ JS如何去除指定字符串

可以用replace函数去复除指定字符串。制

1、在body标签和html标签中添加一个script标签,定义一个字符串,这里以“这是个什么演示文本”为例,将此时的字符串输出到页面:

⑤ 如何用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

⑦ 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如何去除字符串某个特定字符

利用正则表达式配合replace替换指定字符。

语法

stringObject.replace(regexp,replacement)

参数描述
regexp必需。规定了要替换的模式的 RegExp 对象。请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象。
replacement必需。一个字符串值。规定了替换文本或生成替换文本的函数。

返回值

一个新的字符串,是用 replacement 替换了 regexp 的第一次匹配或所有匹配之后得到的。

说明


符串 stringObject 的 replace() 方法执行的是查找并替换的操作。它将在 stringObject 中查找与 regexp
相匹配的子字符串,然后用 replacement 来替换这些子串。如果 regexp 具有全局标志 g,那么 replace()
方法将替换所有匹配的子串。否则,它只替换第一个匹配子串。

replacement 可以是字符串,也可以是函数。如果它是字符串,那么没有匹配都将由字符串替换。但是 replacement 中的 $ 字符具有特定的含义。如下表所示,它说明从模式匹配得到的字符串将用于替换。
字符替换文本
$1、$2、...、$99与 regexp 中的第 1 到第 99 个子表达式相匹配的文本。
$&与 regexp 相匹配的子串。
$`位于匹配子串左侧的文本。
$'位于匹配子串右侧的文本。
%直接量符号。


意:ECMAScript v3 规定,replace() 方法的参数 replacement
可以是函数而不是字符串。在这种情况下,每个匹配都调用该函数,它返回的字符串将作为替换文本使用。该函数的第一个参数是匹配模式的字符串。接下来的参数
是与模式中的子表达式匹配的字符串,可以有 0 个或多个这样的参数。接下来的参数是一个整数,声明了匹配在 stringObject
中出现的位置。最后一个参数是 stringObject 本身。

实例

例子 1

在本例中,我们将使用 "W3School" 替换字符串中的 "Microsoft":

复制代码 代码如下:

<script type="text/javascript">

var str="Visit Microsoft!"
document.write(str.replace(/Microsoft/, "W3School"))

</script>

输出:

Visit W3School!

例子 2

在本例中,我们将执行一次全局替换,每当 "Microsoft" 被找到,它就被替换为 "W3School":

复制代码 代码如下:

<script type="text/javascript">

var str="Welcome to Microsoft! "
str=str + "We are proud to announce that Microsoft has "
str=str + "one of the largest Web Developers sites in the world."

document.write(str.replace(/Microsoft/g, "W3School"))

</script>

输出:

Welcome to W3School! We are proud to announce that W3School
has one of the largest Web Developers sites in the world.

例子 3

您可以使用本例提供的代码来确保匹配字符串大写字符的正确:

复制代码 代码如下:

text = "javascript Tutorial";
text.replace(/javascript/i, "JavaScript");

例子 4

在本例中,我们将把 "Doe, John" 转换为 "John Doe" 的形式:

复制代码 代码如下:

name = "Doe, John";
name.replace(/(\w+)\s*, \s*(\w+)/, "$2 $1");

例子 5

在本例中,我们将把所有的花引号替换为直引号:

复制代码 代码如下:

name = '"a", "b"';
name.replace(/"([^"]*)"/g, "'$1'");

例子 6

在本例中,我们将把字符串中所有单词的首字母都转换为大写:

复制代码 代码如下:

name = 'aaa bbb ccc';
uw=name.replace(/\b\w+\b/g, function(word){
return word.substring(0,1).toUpperCase()+word.substring(1);}
);

例子 7

复制代码 代码如下:

var str="fsaf$a$assdfdasfa$a$dsfadsf";
var strr='\$'+'a'+'\$';
var name = '"a", "b"';
var reger=new RegExp("[\$]a[\$]","gm");

alert(str.replace(reger,'555888'));

⑨ js 怎么去除字符串里面的所有中文 去除所以符号

<script>
var title ="字符串zifuchuan"
var reg=/[\u4E00-\u9FA5]/g;
var result=title.replace(reg,'');
alert(result);
</script>

⑩ js中用正则表达式 过滤特殊字符 校验所有输入域是否含有特殊符号

楼上2位已经说的很明白了,只允许输入规定的字符,如果输入含有其他字符就直接提示,不允许输入特殊字符,或者直接给它替换掉。

阅读全文

与js过滤字符串符号相关的资料

热点内容
用存储卡回提高运行内存速度吗 浏览:496
深圳哪里招污水处理工 浏览:940
哈弗h6工厂滤芯怎么拆 浏览:371
原神纯水精灵怎么捕捉 浏览:109
纯水之球各服务器多少g 浏览:448
格卡诺空气净化器怎么重置 浏览:686
饮水机滤芯怎么验货 浏览:385
华帝净水器售后电话多少 浏览:681
里水污水处理后排到哪里 浏览:6
污水攻坚补齐什么短板 浏览:446
跑污水处理跑业务工资高吗 浏览:894
河南工业污水处理 浏览:871
污水处理厂进水检测标准 浏览:102
天剑摩托车125怎么换机油滤芯 浏览:846
污水处理厂斜管套什么定额 浏览:129
三菱欧蓝德空调滤芯怎么拆换 浏览:386
污水流楼底下怎么处理 浏览:869
汽油滤芯是什么表现 浏览:433
污水消毒处理工艺有哪些 浏览:887
硬水垢清除 浏览:360