過濾標簽的正則表達式
㈠ 求過濾html標簽和CSS樣式表的正則表達式!
先過濾樣式表之後再過濾html標簽
過濾樣式表用
<STYLE>[/s/S]*<\/STYLE>
然後再用<.[^>]*>過濾其他標簽HTML
㈡ 正則表達式如何過濾HTML標簽中的屬性值
去掉html標簽: str.replace(/</?[a-zA-Z]+[^><]*>/g,"")
去掉標簽裡面的屬性: str.replace(/<([a-zA-Z]+)\s*[^><]*>/g,"<$1>")
我親自測試通過,操作語言javascript 樓主內還有問容題的話Hi 我
㈢ 怎樣用正則表達式過濾掉頁面中除了<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)
㈣ 用正則表達式過濾HTML標簽
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* <p>
* Title: HTML相關的正則表達式工具類
* </p>
* <p>
* Description: 包括過濾HTML標記,轉換HTML標記,替換特定HTML標記
* </p>
* <p>
* Copyright: Copyright (c) 2006
* </p>
*
* @author hejian
* @version 1.0
* @createtime 2006-10-16
*/
public class HtmlRegexpUtil {
private final static String regxpForHtml = "<([^>]*)>"; // 過濾所有以<開頭以>結尾的標簽
private final static String regxpForImgTag = "<\\s*img\\s+([^>]*)\\s*>"; // 找出IMG標簽
private final static String regxpForImaTagSrcAttrib = "src=\"([^\"]+)\""; // 找出IMG標簽的SRC屬性
/**
*
*/
public HtmlRegexpUtil() {
// TODO Auto-generated constructor stub
}
/**
*
* 基本功能:替換標記以正常顯示
* <p>
*
* @param input
* @return String
*/
public String replaceTag(String input) {
if (!hasSpecialChars(input)) {
return input;
}
StringBuffer filtered = new StringBuffer(input.length());
char c;
for (int i = 0; i <= input.length() - 1; i++) {
c = input.charAt(i);
switch (c) {
case '<':
filtered.append("<");
break;
case '>':
filtered.append(">");
break;
case '"':
filtered.append(""");
break;
case '&':
filtered.append("&");
break;
default:
filtered.append(c);
}
}
return (filtered.toString());
}
/**
*
* 基本功能:判斷標記是否存在
* <p>
*
* @param input
* @return boolean
*/
public boolean hasSpecialChars(String input) {
boolean flag = false;
if ((input != null) && (input.length() > 0)) {
char c;
for (int i = 0; i <= input.length() - 1; i++) {
c = input.charAt(i);
switch (c) {
case '>':
flag = true;
break;
case '<':
flag = true;
break;
case '"':
flag = true;
break;
case '&':
flag = true;
break;
}
}
}
return flag;
}
/**
*
* 基本功能:過濾所有以"<"開頭以">"結尾的標簽
* <p>
*
* @param str
* @return String
*/
public static String filterHtml(String str) {
Pattern pattern = Pattern.compile(regxpForHtml);
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result1 = matcher.find();
while (result1) {
matcher.appendReplacement(sb, "");
result1 = matcher.find();
}
matcher.appendTail(sb);
return sb.toString();
}
/**
*
* 基本功能:過濾指定標簽
* <p>
*
* @param str
* @param tag
* 指定標簽
* @return String
*/
public static String fiterHtmlTag(String str, String tag) {
String regxp = "<\\s*" + tag + "\\s+([^>]*)\\s*>";
Pattern pattern = Pattern.compile(regxp);
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result1 = matcher.find();
while (result1) {
matcher.appendReplacement(sb, "");
result1 = matcher.find();
}
matcher.appendTail(sb);
return sb.toString();
}
/**
*
* 基本功能:替換指定的標簽
* <p>
*
* @param str
* @param beforeTag
* 要替換的標簽
* @param tagAttrib
* 要替換的標簽屬性值
* @param startTag
* 新標簽開始標記
* @param endTag
* 新標簽結束標記
* @return String
* @如:替換img標簽的src屬性值為[img]屬性值[/img]
*/
public static String replaceHtmlTag(String str, String beforeTag,
String tagAttrib, String startTag, String endTag) {
String regxpForTag = "<\\s*" + beforeTag + "\\s+([^>]*)\\s*>";
String regxpForTagAttrib = tagAttrib + "=\"([^\"]+)\"";
Pattern patternForTag = Pattern.compile(regxpForTag);
Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib);
Matcher matcherForTag = patternForTag.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result = matcherForTag.find();
while (result) {
StringBuffer sbreplace = new StringBuffer();
Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag
.group(1));
if (matcherForAttrib.find()) {
matcherForAttrib.appendReplacement(sbreplace, startTag
+ matcherForAttrib.group(1) + endTag);
}
matcherForTag.appendReplacement(sb, sbreplace.toString());
result = matcherForTag.find();
}
matcherForTag.appendTail(sb);
return sb.toString();
}
}
㈤ 要去掉<p></p>標簽的正則表達式怎麼寫
方法:
查找 </?[p|P][^>]*> 替換成空字元串即可。
具體如下:
protected string str = "<table><tr><td>sdasasdsdd</td></tr></table><br><p>sds</p><img id='img1' src='http://www..com/img/_logo.gif' width='100' height='50' alt=''>aaassss<br><img src='http://www..com/img/_logo.gif' width='100' height='50' alt=''> 說是道 ";
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);
}
㈥ JavaScript用正則表達式過濾 html 標簽
1:content=content.replace(/<\/?p[^>]*>/gi,'');//刪除所有的回<p>及</p>標簽答
2:content=content.replace(/<p[^>]*>/gi,'').replace(/<\/p>/gi,'<br />');
㈦ 如何過濾html標簽中的正則表達式
如何過濾html標簽中的正則表達式
先過濾樣式表之後再過濾html標簽
過濾樣式表用
<STYLE>[/s/S]*<\/STYLE>
然後再用<.[^>]*>過濾其他標簽HTML
㈧ 正則表達式過濾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
<%
Function stripHTML(strHTML)
'Strips the HTML tags from strHTML
Dim objRegExp, strOutput
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "<.+?>"
'Replace all HTML tag matches with the empty string
strOutput = objRegExp.Replace(strHTML, "")
'Replace all < and > with < and >
strOutput = Replace(strOutput, "<", "<")
strOutput = Replace(strOutput, ">", ">")
stripHTML = strOutput 'Return the value of strOutput
Set objRegExp = Nothing
End Function
%>
㈩ 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)*?>)",
"
");
//
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;
}