召唤正则表达式达人:去掉HTML 需求如下内容发布系统编辑内容的时候会含有HTML但我现在要在前台展示的时候,需要展示标题和一小部分内容如果只是单单的截取,那么会破坏到HTML现在要把HTML去掉,只申请文字,在截取求去掉HTML的正则表达式 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary> /// 移除Html标记 /// </summary> /// <param name="content"></param> /// <returns></returns> public static string RemoveHtml(string content) { return Regex.Replace(content, @"<[^>]*>", string.Empty, RegexOptions.IgnoreCase); } /// <summary> /// 从HTML中获取文本,保留br,p,img /// </summary> /// <param name="HTML"></param> /// <returns></returns> public static string GetTextFromHTML(string HTML) { System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(@"</?(?!br|/?p|img)[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); return regEx.Replace(HTML, ""); } /// <summary> /// 去掉html标记 /// </summary> /// <param name="str"></param> /// <returns></returns> protected static string ConvertGettext(string str) { Regex regex = new Regex(@"\<(.*?)\>", RegexOptions.IgnoreCase); return regex.Replace(str, "").Replace(" ", "").Replace("\n", "").Replace("\r", ""); } 不是很明白你的意思去掉htmlstring html="你的html";html=Regex.Replace(html,@"(?is)<(\w+)>.*?</\1>","") html=Regex.Replace(html,@"(?is)\<(.*?)/?\>","") 40分啦,,,简单问题 工作两年没用过wenservers需要详细的教程,解答谢谢给个参考链接 问两个关于 Response.Write 和 Response.Redirect 的问题。谢谢。(附伪码) IIS6.0如何对.*做映射啊? dropdownlist能用web.sitemap吗? DataGrid 另存成 Excel的问题 招聘asp.net开发人员。 几个面试问题,大家一起来做做 请问一个问题,我是初学者,我的码错在那里 有学习asp.net的好书吗? 问一个正则表达式 Repository模式或 IDAL DAL怎么实现IOC依赖注入
/// 移除Html标记
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static string RemoveHtml(string content)
{
return Regex.Replace(content, @"<[^>]*>", string.Empty, RegexOptions.IgnoreCase);
}
/// <summary>
/// 从HTML中获取文本,保留br,p,img
/// </summary>
/// <param name="HTML"></param>
/// <returns></returns>
public static string GetTextFromHTML(string HTML)
{
System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(@"</?(?!br|/?p|img)[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); return regEx.Replace(HTML, "");
}
/// 去掉html标记
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
protected static string ConvertGettext(string str)
{
Regex regex = new Regex(@"\<(.*?)\>", RegexOptions.IgnoreCase); return regex.Replace(str, "").Replace(" ", "").Replace("\n", "").Replace("\r", "");
}
去掉html
string html="你的html";
html=Regex.Replace(html,@"(?is)<(\w+)>.*?</\1>","")