導航:首頁 > 凈水問答 > js正則過濾標簽

js正則過濾標簽

發布時間:2022-07-23 19:15:36

Ⅰ 如何使用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>

Ⅱ Js字元串的正則匹配 如何過濾掉指定特徵的字元串

String.replace(正則表達式,"")
replace是string類型內置的替換方法,第一個參數可以是正則表達式,第二個版參數是想權要替換成的文本,正則中可以使用/g來表示替換所有匹配的文本,不使用則代表只替換匹配到的第一個字元對象,將第二個參數設為空字元串便可達到過濾的效果。
具體正則需要你自己去了解關於正則的知識了,祝你好運。

Ⅲ 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|][r|R]/*>)+|(<[p|P](.|/n)*?>)",
"
");
//
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;
}

Ⅳ 如何採用JS正則過濾以下代碼

varhtml='<p><spanstyle="text-indent:43px;font-size:21px;font-family:宋體;">11</span><spanstyle="text-indent:43px;font-size:21px;font-family:宋體;">月<span>26</span>日,黨委副書記帶領學校部分獲得教學能手的教師到初級中學,學生的生活、學習送去「溫暖」。</span></p>
<pstyle="text-indent:43px;mso-char-indent-count:2.0"><spanstyle="font-size:21px;font-family:宋體;">為幫助初中學生抵禦嚴寒,捐贈<span>5000</span>元現金,希望他們能在寒冷的冬季克服困難,認真踏實學習。</span></p>
<pstyle="text-indent:43px;text-align:center;"><imgsrc="/uploadfiles/CMS/image/1448529129738_0.jpg"width="665"height="500"alt=""/></p>

<pstyle="text-indent:43px;text-align:center;"><imgsrc="/uploadfiles/CMS/image/1448529242675_0.jpg"width="665"height="500"alt=""/></p>
<pstyle="text-indent:43px;mso-char-indent-count:2.0"><spanstyle="font-size:21px;font-family:宋體;mso-ascii-theme-font:major-fareast;


mso-fareast-theme-font:major-fareast;mso-hansi-theme-font:major-fareast"><o:p></o:p></span></p>';
varreg=/</?(?:(?!p|img)[^>]*)>/gi;
html=html.replace(reg,"");
console.log(html);

Ⅳ 怎樣用正則表達式過濾掉頁面中除了<p></p>和<img>以外所有的標簽

這個還真不容易實現,單獨保留p或者img都可以,但是兩個條件放一起就不行了。於專是我換屬了一種思路,用了個函數實現了,你看下,代碼是python下的:

importre

t='<html>asdfasdf<head>1111111111<body><p>asdfasdfasdf</p><imgherf="fff">'
defreplace_two(m):
"""
#過濾掉頁面中除了<p></p>和<img>以外所有的標簽
"""
all=re.findall(r'</?.*?>',m)
save=re.findall(r'</?(?:img).*?>|</?[pP]*?>',m)

foreinall:
ifenotinsave:
m1=m.replace(e,'')
m=m1
returnm

printreplace_two(t)

Ⅵ 在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>

Ⅶ 正則js過濾除p以外標簽

是連p以外的標簽的內容都不要麼?


如果是的話,可以反過來想,只要匹配所有p標簽以及其以內的內容即可,那麼可以這么寫

varre=newRegExp("<p>\w*</p>","g");
result=str.match(re)

str是你所需要進行匹配的字元串,如果是頁面全部,就獲取body下所有的內容就好

result是一個數組,而每一個元素就是你匹配對象中的p標簽以及其內容

Ⅷ 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標記
}

Ⅸ 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或者jQuery去除掉某個標簽但是不去除裡面的內容

1、可以使用正則直接取到img

//思路分兩步:
//1,匹配出圖片img標簽(即匹配出所有圖片),過濾其他不需要的字元
//2.從匹配出來的結果(img標簽中)循環匹配出圖片地址(即src屬性)
varstr="<td>thisisteststring<imgsrc="http:yourweb.com/test.jpg"width='50'>123andtheend<imgsrc="所有地址也能匹配.jpg"/>33!<imgsrc="/uploads/attached/image/20120426/20120426225658_92565.png"alt=""/></td>"
//匹配圖片(g表示匹配所有結果i表示區分大小寫)
varimgReg=/<img.*?(?:>|/>)/gi;
//匹配src屬性
varsrcReg=/src=['"]?([^'"]*)['"]?/i;
vararr=str.match(imgReg);
alert('所有已成功匹配圖片的數組:'+arr);
for(vari=0;i<arr.length;i++){
varsrc=arr[i].match(srcReg);
//獲取圖片地址
if(src[1]){
alert('已匹配的圖片地址'+(i+1)+':'+src[1]);
}
//當然你也可以替換src屬性
if(src[0]){
vart=src[0].replace(/src/i,"href");
//alert(t);
}
}
閱讀全文

與js正則過濾標簽相關的資料

熱點內容
mc全世界都是核廢水該怎麼生存 瀏覽:876
老式沁園飲水機多少錢 瀏覽:907
利尊潛水污水泵價格 瀏覽:260
東風m3的空氣濾芯在哪裡 瀏覽:66
飲水機里出來的絮狀東西是什麼 瀏覽:29
杭州污水池環氧防腐漆價格 瀏覽:732
如何提升路由器上傳速度 瀏覽:117
超濾凈水設備廠商代理 瀏覽:827
edi按功能分為有哪幾種 瀏覽:634
惠普康反滲透純水機 瀏覽:226
空氣濾芯是濾什麼的 瀏覽:124
長按出水的飲水機怎麼按 瀏覽:872
水處理工理論知識 瀏覽:602
大家都放不開純凈水什麼意思 瀏覽:215
容聲凈水器308多少錢 瀏覽:37
鈦棒過濾芯使用壽命 瀏覽:585
冷固化樹脂攪拌不均勻 瀏覽:158
光電信息產業廢水 瀏覽:514
嬰兒誤食檸檬酸除垢劑殘留 瀏覽:275
鈣鎂離子濾芯怎麼樣 瀏覽:748