清除文本中Html的标签

/// <summary>
/// 清除文本中Html的标签
/// </summary>
/// <param name="Content"></param>
/// <returns></returns>
protected string ClearHtml(string Content)
{
    Content = ReplaceHtml("&#[^>]*;", "", Content);
    Content = ReplaceHtml("</?marquee[^>]*>", "", Content);
    Content = ReplaceHtml("</?object[^>]*>", "", Content);
    Content = ReplaceHtml("</?param[^>]*>", "", Content);
    Content = ReplaceHtml("</?embed[^>]*>", "", Content);
    Content = ReplaceHtml("</?table[^>]*>", "", Content);
    Content = ReplaceHtml(" ", "", Content);
    Content = ReplaceHtml("</?tr[^>]*>", "", Content);
    Content = ReplaceHtml("</?th[^>]*>", "", Content);
    Content = ReplaceHtml("</?p[^>]*>", "", Content);
    Content = ReplaceHtml("</?a[^>]*>", "", Content);
    Content = ReplaceHtml("</?img[^>]*>", "", Content);
    Content = ReplaceHtml("</?tbody[^>]*>", "", Content);
    Content = ReplaceHtml("</?li[^>]*>", "", Content);
    Content = ReplaceHtml("</?span[^>]*>", "", Content);
    Content = ReplaceHtml("</?div[^>]*>", "", Content);
    Content = ReplaceHtml("</?th[^>]*>", "", Content);
    Content = ReplaceHtml("</?td[^>]*>", "", Content);
    Content = ReplaceHtml("</?script[^>]*>", "", Content);
    Content = ReplaceHtml("(javascript|jscript|vbscript|vbs):", "", Content);
    Content = ReplaceHtml("on(mouse|exit|error|click|key)", "", Content);
    Content = ReplaceHtml("<\\?xml[^>]*>", "", Content);
    Content = ReplaceHtml("<\\/?[a-z]+:[^>]*>", "", Content);
    Content = ReplaceHtml("</?font[^>]*>", "", Content);
    Content = ReplaceHtml("</?b[^>]*>", "", Content);
    Content = ReplaceHtml("</?u[^>]*>", "", Content);
    Content = ReplaceHtml("</?i[^>]*>", "", Content);
    Content = ReplaceHtml("</?strong[^>]*>", "", Content);
    string clearHtml = Content;
    return clearHtml;
}
/// <summary>
/// 清除文本中的Html标签
/// </summary>
/// <param name="patrn">要替换的标签正则表达式</param>
/// <param name="strRep">替换为的内容</param>
/// <param name="content">要替换的内容</param>
/// <returns></returns>
private string ReplaceHtml(string patrn, string strRep, string content)
{
    if (string.IsNullOrEmpty(content))
    {
        content = "";
    }
    Regex rgEx = new Regex(patrn, RegexOptions.IgnoreCase);
    string strTxt = rgEx.Replace(content, strRep);
    return strTxt;
}
                  

原文地址:https://www.cnblogs.com/TTonly/p/12082927.html

时间: 2024-08-01 03:07:46

清除文本中Html的标签的相关文章

C# 清除文本中的HTML标签

/// <summary>          /// 清除文本中Html的标签          /// </summary>          /// <param name="Content"></param>          /// <returns></returns>          public static string ClearHtml(string Content)          { 

去除富文本中的html标签及vue、react、微信小程序中的过滤器

在获取富文本后,又只要显示部分内容,需要去除富文本标签,然后再截取其中一部分内容:然后就是过滤器,在微信小程序中使用还是挺多次的,在vue及react中也遇到过 1.富文本去除html标签 去除html标签及?空格 let richText = ' <p style="font-size: 25px;color: white">&nbsp; &nbsp; &nbsp; &nbsp;sdaflsjf的丰富及饿哦塞尔</p><s

去除文本中的HTML标签、中英文标点符号、数字及英文单词

在进行中文分词统计前,往往要先把爬取下来的文本中包含的一些标签.标点符号.英文字母等过滤掉,这一过程叫做数据清洗. #coding=utf-8 import re import codecs def strs_filter(file): with codecs.open(file,"r","utf8") as f,codecs.open("result.txt","a+","utf8") as c: lin

Android检测富文本中的&lt;img标签并实现点击效果

本文旨在:通过点击一张图片Toast输出位置与url链接. 闲话少说,实现原理大概是酱紫的::通过正则表达式检测富文本内的图片集合并获取url,在src=“xxx” 后面添加 onclick方法,至于js如何load进去本人是自己拼接了一个html标签的上下文 js调用java方法请自行搜索不在本文讨论范围. public class HtmlUtils { /** * 获取html中的所有图片 * @param compatText * @return */ public static Lis

点击鼠标清除文本框中的内容

点击鼠标清除文本框中的内容:一般内容型的网站都有搜索功能,在默认状态下搜索框中都有默认的提示文本,例如“请输入要搜索的关键词”,但当搜索框获得焦点的时候,默认的提示文本内容会被清空,下面就简单介绍以下如何实现此功能,代码实例如下: <!<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="

如何使用JS脚本从HTML中分离图片标签与文本,替换文本中指定的内容并加粗(原创)

var html='ddfsdfsdfdsd dfsdfsdffds<img _src="http://localhost:8490/60E86EA7-FE7B-44BF-8270-4DF2036B2118.PNG" data-latex="x=\frac {-b\pm \sqrt {{b}^{2}-4ac}} {2a}" src="http://localhost:8490/60E86EA7-FE7B-44BF-8270-4DF2036B2118.

C#怎么清除字符串中HTML标签。。。

因为用到了一款编辑器的原因,使得数据库中保存的数据会夹杂着一些HTML标签,之后导出的数据中就会出现一些不同的HTML的标签.严重影响用户的视觉体验(主要自己都看不下去了)... 下面是我将DataTable进行重新组装,清除HTML标签的方法: 1 /// <summary> 2 /// 重新组装DataTable(主要是为了清除DataTable数据中的HTML标签) 3 /// </summary> 4 /// <param name="new_dt"

HTML中的&lt;select&gt;标签如何设置默认选中的选项

方法有两种. 第一种通过<select>的属性来设置选中项,此方法可以在动态语言如php在后台根据需要控制输出结果. 1 2 3 4 5 < select  id =  "sel" > < option  value = "1" >1</ option > < option  value = "2"  selected = "selected" >2</ opt

vim技巧4 删除/保留文本中匹配行

vim技巧:如何删除/保留文本中特定的行呢? <ol><a href="/ss/ss/www"> show invisibles</a> <a href="/ss/ssg/gg/ff/"> one world one dream</a> 任务1:只保留<a>标签内的标题,而把其他行都删掉:g/re/d 知识点:使用:global与:delete命令 运行一下命令: /\v\<\ /?\w+