導航:首頁 > 凈水問答 > 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非法字元相關的資料

熱點內容
為什麼飛利浦凈化器老是紅燈 瀏覽:363
海關edi是什麼費用 瀏覽:834
過濾器濾芯為什麼爆掉 瀏覽:444
凈水器怎麼換ppf濾芯 瀏覽:635
ro膜能濾掉水垢 瀏覽:311
為什麼有的純凈水泡茶不變色 瀏覽:733
電水壺水垢爆炸聲 瀏覽:315
邢台王快污水處理提標 瀏覽:922
一體化污水拼裝罐 瀏覽:340
恆壓供水反滲透水處理成套設備價格 瀏覽:977
污水管網建議的請示 瀏覽:13
超濾能過濾水鹼嗎 瀏覽:11
ro膜為什麼容易堵塞 瀏覽:385
gpd反滲透膜是什麼意思 瀏覽:111
怎樣清理殼管冷凝器的水垢 瀏覽:993
蒸餾中如何收集沸點高的餾分 瀏覽:489
哈弗276換空調濾芯怎麼換 瀏覽:363
反滲透各段之間的壓力 瀏覽:644
污水排出管穿外牆用什麼套管 瀏覽:571
污水處理候能養魚嗎 瀏覽:436