在线求助,请高手帮忙写一个简单的正则表达式以过滤HTML代码。 用于剔除内容里 "<" ">"之间的HTML代码,以保证显示在网页中的都是实际内容,不带一点HTML代码。问题解决,结帖给分。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 试下string yourStr = ............;string resultStr = Regex.Replace(yourStr, @"<[\s\S]*?>","" , RegexOptions.IgnoreCase); <.+?>可匹配所有html标签! string str = "<a>DSFDSF<B>fasf<img>dsfaf<td>dfaf</td>"; Response.Write(System.Text.RegularExpressions.Regex.Replace(str,"<([^>]*?)>","")); using System;using System.Web;using System.Collections.Generic;using System.Text;using System.Text.RegularExpressions;namespace TopWin.Common{ /// <summary> /// 处理HTML /// </summary> public class HTML { #region 去除HTML标记 /// <summary> /// 去除HTML标记 /// </summary> /// <param name="Htmlstring">包括HTML的源码 </param> /// <returns>已经去除后的文字</returns> public static string NoHTML(string Htmlstring) { if (Htmlstring == null) { return ""; } //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase); //删除HTML Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase); Htmlstring.Replace("<", ""); Htmlstring.Replace(">", ""); Htmlstring.Replace("\r\n", ""); //防止有漏掉的HTML代码 Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim(); return Htmlstring; } #endregion /// <summary> /// 将C#中的回车(\r)和空格(" ")换成HTML格式对应的换行和空格 /// </summary> /// <param name="strInPut"></param> /// <returns></returns> static public string Text2Html(string strInPut) { StringBuilder Temp = new StringBuilder(); Temp.Append(strInPut); Temp.Replace(" ", " "); Temp.Replace("<", "<"); Temp.Replace(">", ">"); Temp.Replace("\r", "<br>"); //Temp.Replace("&","&"); Temp.Replace("\"", """); return Temp.ToString(); } /// <summary> /// 将HTML格式的换行和空格换成C#中的对应字符("\r"、" ") /// </summary> /// <param name="strInPut"></param> /// <returns></returns> static public string Html2Text(string strInPut) { StringBuilder Temp = new StringBuilder(); Temp.Append(strInPut); Temp.Replace("<br>", "\r"); Temp.Replace(" ", " "); Temp.Replace("<", "<"); Temp.Replace(">", ">"); //Temp.Replace("&","&"); Temp.Replace(""", "\""); return Temp.ToString(); } }} HTML里面的脚本、热点、注释,全部删除 两个??问号 连在一起是什么意思? C# asp.net Gridview的使用问题 asp.net 主机带宽负荷问题 gridview中某列如何展现比例图形? activeReports 报表的问题 在线等啊,急+++++(50分) 请教一个上传文件的问题,麻烦了~~~~ 求一在线编辑器(可以上传图片的) .net 中实现css 请教如何实现一个问答功能? 关于数据访问层支持(access和sqlserver)的问题 关于showModalDialog的问题 NHibernate的问题
string resultStr = Regex.Replace(yourStr, @"<[\s\S]*?>","" , RegexOptions.IgnoreCase);
Response.Write(System.Text.RegularExpressions.Regex.Replace(str,"<([^>]*?)>",""));
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace TopWin.Common
{
/// <summary>
/// 处理HTML
/// </summary>
public class HTML
{
#region 去除HTML标记
/// <summary>
/// 去除HTML标记
/// </summary>
/// <param name="Htmlstring">包括HTML的源码 </param>
/// <returns>已经去除后的文字</returns>
public static string NoHTML(string Htmlstring)
{
if (Htmlstring == null)
{
return "";
}
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase); Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
//防止有漏掉的HTML代码
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim(); return Htmlstring;
}
#endregion /// <summary>
/// 将C#中的回车(\r)和空格(" ")换成HTML格式对应的换行和空格
/// </summary>
/// <param name="strInPut"></param>
/// <returns></returns>
static public string Text2Html(string strInPut)
{
StringBuilder Temp = new StringBuilder();
Temp.Append(strInPut); Temp.Replace(" ", " ");
Temp.Replace("<", "<");
Temp.Replace(">", ">");
Temp.Replace("\r", "<br>");
//Temp.Replace("&","&");
Temp.Replace("\"", """);
return Temp.ToString();
} /// <summary>
/// 将HTML格式的换行和空格换成C#中的对应字符("\r"、" ")
/// </summary>
/// <param name="strInPut"></param>
/// <returns></returns>
static public string Html2Text(string strInPut)
{
StringBuilder Temp = new StringBuilder();
Temp.Append(strInPut); Temp.Replace("<br>", "\r");
Temp.Replace(" ", " ");
Temp.Replace("<", "<");
Temp.Replace(">", ">");
//Temp.Replace("&","&");
Temp.Replace(""", "\"");
return Temp.ToString();
} }
}