导航:首页 > 净水问答 > js过滤html非法字符

js过滤html非法字符

发布时间:2023-06-18 23:16:04

A. 如何使用js正则 过滤某一个html标签下所有的标签跟样式呢只保留出纯文本

js过滤标签的方法。分享给大家供大家参考,具体如下:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>无标题文档</title>
<script>
window.onload=function()
{
varoTxt1=document.getElementById('txt1');
varoTxt2=document.getElementById('txt2');
varoBtn=document.getElementById('btn');
oBtn.onclick=function()
{
varreg=/<[^<>]+>/g;
oTxt2.value=oTxt1.value.replace(reg,'');
};
};
</script>
</head>
<body>
<textareaid="txt1"cols="40"rows="10"></textarea><br/>
<inputtype="button"value="过滤"id="btn"/><br/>
<textareaid="txt2"cols="40"rows="10"></textarea>
</body>
</html>

B. 过滤网页上输入的非法字符,从网上找了段js代码,但是发现我用中文输入时就逃过了过滤,这个该怎么解决呢

因为中文输入的特殊字符已经失去了特殊控制意义,所以程序没有过滤中文输入的这些符号,如果你中文的符号也不放过,那么需要修改下面这个语句:
var txt=new RegExp("[ ,\\`,\\~,\\!,\\@,\#,\\$,\\%,\\^,\\+,\\*,\\&,\\\\,\\/,\\?,\\|,\\:,\\.,\\<,\\>,\\{,\\},\\(,\\),\\',\\;,\\=,\"]");

例如增加过滤@符号的的语句如下:
var txt=new RegExp("[ ,\\`,\\~,\\!,\\@,\#,\\$,\\%,\\^,\\+,\\*,\\&,\\\\,\\/,\\?,\\|,\\:,\\.,\\<,\\>,\\{,\\},\\(,\\),\\',\\;,\\=,\"@]");

添加到]前面就可以了,好像转换了中文和英文的符号,你在记事本里面修改程序代码的时候注意区别。

C. JS正则过滤指定的HTML标签

1,得到网页上的链接源地址:

string
matchString =
@"<a[^>]+href=\s*(?:'(?<href>[^']+)'|""(?<href>[^""]+)""|(?<href>[^>\s]+))\s*[^>]*>";
2,得到网页的标题:
string matchString = @"<title>(?<title>.*)</title>";
3,去掉网页中的所有的html标记:
string temp = Regex.Replace(html, "<[^>]*>", ""); //html是一个要去除html标记的文档

4, string matchString = @"<title>([\S\s\t]*?)</title>";
5,js去掉所有html标记的函数:
function delHtmlTag(str)
{
return str.replace(/<[^>]+>/g,"");//去掉所有的html标记
}

D. 在javascript中用正则表达式过滤指定的字符(一定要能指定!)

楼上的不加转义字符\ 你们搞什么啊
正确的应该是这样的

加入你得到的内字符窜容为 name
<html>
<head>
<script>
function test1(){
var name=document.getElementById('user').value;
name=name.replace(/(\!+)|(\<+)|(\>+)|(\'+)/g,"");
alert(name);
}
</script>
</head>

<body>
<input type="text" id="user" />
<input type="button" value="te" onclick="test1()">
</body>
</html>

E. 怎么让js过滤html标签

|代码袭如下:
function removeHTMLTag(str) {
str = str.replace(/<\/?[^>]*>/g,''); //去除HTML tag
str = str.replace(/[ | ]*\n/g,'\n'); //去除行尾空白
//str = str.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
str=str.replace(/ /ig,'');//去掉
return str;
}

F. 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

G. js怎样过滤html里的字符串并显示

var len=document.getElementsByTagName('a').length
for(var i=0;i<len;i++){
document.getElementsByTagName('a')[i].style.display="none"
}

H. js正则表达式过滤html标签,这个正则式怎么写

代码虽短功能却超强,运行效率也很高!
public static string ClearHtmlCode(string text)
{
text = text.Trim();
if (string.IsNullOrEmpty(text))
return string.Empty;
text = Regex.Replace(text, "[/s]{2,}", " "); //two or more spaces
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|/n)*?>)", " "); //<br>
text = Regex.Replace(text, "(/s*&[n|N][b|B][s|S][p|P];/s*)+", " "); //
text = Regex.Replace(text, "<(.|/n)*?>", string.Empty); //any other tags
text = Regex.Replace(text, "/<//?[^>]*>/g", string.Empty); //any other tags
text = Regex.Replace(text, "/[ | ]* /g", string.Empty); //any other tags
text = text.Replace("'", "''");
text = Regex.Replace(text, "/ [/s| | ]* /g", string.Empty);
return text;
}

I. jsp中如何过滤非法字符

可以利用javascript验证正则表达式的方式对非法字符做处理,同样Java也可以利用正则表达式帮你过滤非法字符,最终还得更具你的具体需求来定。

J. 如何用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过滤html非法字符相关的资料

热点内容
为什么有的纯净水泡茶不变色 浏览:733
电水壶水垢爆炸声 浏览:315
邢台王快污水处理提标 浏览:922
一体化污水拼装罐 浏览:340
恒压供水反渗透水处理成套设备价格 浏览:977
污水管网建议的请示 浏览:13
超滤能过滤水碱吗 浏览:11
ro膜为什么容易堵塞 浏览:385
gpd反渗透膜是什么意思 浏览:111
怎样清理壳管冷凝器的水垢 浏览:993
蒸馏中如何收集沸点高的馏分 浏览:489
哈弗276换空调滤芯怎么换 浏览:363
反渗透各段之间的压力 浏览:644
污水排出管穿外墙用什么套管 浏览:571
污水处理候能养鱼吗 浏览:436
遵化国祯污水厂的员工待遇怎么样 浏览:994
海尔纯净水报警e007什么意思 浏览:188
嘉年华三厢空气滤芯怎么拆 浏览:512
ro膜脱盐率啥意思 浏览:235
男人用这种方式回微信 浏览:775