• 主页
  • ASP
  • .NET Framework
  • Web Services
  • VB
  • VC
  • 图表区
  • 分析与设计
  • 组件/控件开发
  • LINQ
RAAS39529 2014年01月07日
(string Htmlstring)能否保留<a> <b> <p> <strong> <img> <script src="/*.js"></script>

能否保留<a> <b> <p> <strong> <img> <script src="/*.js"></script>这几个标签呢


public static string NoHTML(string Htmlstring)
{

//删除脚本

Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);

//删除HTML
// Htmlstring.Replace("</p>", "</p>\r\n");
Htmlstring = Regex.Replace(Htmlstring, @"</p>", "</p>:hn", RegexOptions.IgnoreCase);
// Htmlstring = Regex.Replace(Htmlstring, @"<div>", "", RegexOptions.IgnoreCase);
// Htmlstring = Regex.Replace(Htmlstring, @"</div>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<(meta)[^>]*>", "", RegexOptions.IgnoreCase);

Htmlstring.Replace("<div>", "");
Htmlstring.Replace("</div>", "");
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);

// Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "<br>", 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, @"&(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, @"(\r\n)*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"(\s)", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(rdquo);", "”", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(ldquo);", "“", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(mdash);", "—", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(hellip);", "…", RegexOptions.IgnoreCase);
Htmlstring.Replace("<", "");

Htmlstring.Replace(">", "");

Htmlstring = Regex.Replace(Htmlstring, @":hn", "<br>  ", RegexOptions.IgnoreCase);
// Htmlstring.Replace(":hn", "<br>");
// Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
// Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
// Htmlstring = Regex.Replace(Htmlstring, @"<", "<", RegexOptions.IgnoreCase);
// Htmlstring = Regex.Replace(Htmlstring, @">", ">", RegexOptions.IgnoreCase);
// Htmlstring.Replace("<", "<");
// Htmlstring.Replace(">", ">");
// Htmlstring = System.Web.HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();


return Htmlstring;


}



...全文
131 点赞 收藏 1
写回复
1 条回复

还没有回复,快来抢沙发~

发动态
发帖子
.NET技术社区
创建于2007-09-28

4.8w+

社区成员

66.8w+

社区内容

.NET技术交流专区
社区公告
暂无公告