js正則過濾html標簽
『壹』 怎麼使用js過濾html標簽
你可以利用正則表達式來剔除這些標簽,也就是將所有的html類的標簽都替換為空即可:
//去除HTML標簽
str=str.replace(/</?[^>]*>/g,'');
『貳』 如何用正則表達式去掉html標簽
^protected void Page_Load(object sender, EventArgs e)
{
//string regexstr = @"<[^>]*>"; //去除所有的標簽
//@"<script[^>]*?>.*?</script >" //去除所有腳本,中間部分內也刪除
// string regexstr = @"<img[^>]*>"; //去除圖片的正容則
// string regexstr = @"<(?!br).*?>"; //去除所有標簽,只剩br
// string regexstr = @"<table[^>]*?>.*?</table>"; //去除table裡面的所有內容
string regexstr = @"<(?!img|br|p|/p).*?>"; //去除所有標簽,只剩img,br,p
str = Regex.Replace(str, regexstr, string.Empty, RegexOptions.IgnoreCase);
}
『叄』 js 正則表達式去除指定的HTML標簽
可以這么寫:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Strict//EN""
<htmlxmlns="
<head>
<title>匹配正則表達式</title>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<scripttype="text/javascript">
functiont1(){
varcont=document.getElementById('cont');
varcv=cont.value;
varreg=/<a[s]+[^>]+>([^<>]+)</a>/gi;//正則表達式
alert(cv.replace(reg,''));
}
</script>
<styletype="text/css">
textarea{
width:400px;
height:200px;
}
</style>
</head>
<body>
<p>
<textareaid="cont"></textarea>
</p>
<p><inputtype="button"value="把鏈接換成空鏈接"onclick="t1();"/></p>
</body>
</html>
『肆』 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;
}
『伍』 如何使用js正則 過濾某一個html標簽下所有的標簽跟樣式呢只保留出純文本
js過濾HTML標簽的方法。分享給大家供大家參考,具體如下:
<!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 如何用正則替換指定HTML標簽
vars="<span>a</span>....<span>z</span>";
s=s.replace(/<span>(.*?)</span>/g,"<inputtype='text'value='$1'/>")
『柒』 正則表達式過濾html標簽和標簽中的注釋
|using System;
// 不過仔細看,我這個也沒錯啊...
// MyRule=@"<(?:[^><]|""[^""]""|'[^']')*>";
// 實際檢驗一下專:
using System.Text.RegularExpressions;
class Test
{
static void Main()
{
string s = @"<td onmouseover=""if (a > 0)"">abc</td>def";
string r = @"<(?:[^><]|""[^""]""|'[^']')*>";
string t = Regex.Replace(s, r, "");
Console.WriteLine(t); // 輸出:「屬 0)">abcdef」
}
}
『捌』 正則表達式如何過濾HTML標簽中的屬性值
去掉html標簽: str.replace(/</?[a-zA-Z]+[^><]*>/g,"")
去掉標簽裡面的屬性: str.replace(/<([a-zA-Z]+)\s*[^><]*>/g,"<$1>")
我親自測試通過,操作語言javascript 樓主內還有問容題的話Hi 我
『玖』 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標記
}
『拾』 JavaScript用正則表達式過濾 html 標簽
1:content=content.replace(/<\/?p[^>]*>/gi,'');//刪除所有的回<p>及</p>標簽答
2:content=content.replace(/<p[^>]*>/gi,'').replace(/<\/p>/gi,'<br />');