过滤字符串中的html标签

C#中,我们有时需要过滤掉字符串中的部分html标签,以下是一些简单的html标签过滤方法,使用的主要方式是正则表达式

public static string ClearHtml(string html)
        {
           if(string.IsNullOrEmpty(html))
            {
                return "";
            }
            //去除a标签
            html = Regex.Replace(html, @"<a\s*[^>]*>", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"</a>", "", RegexOptions.IgnoreCase);
            //去除span标签
            html = Regex.Replace(html, @"<span\s*[^>]*>", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"</span>", "", RegexOptions.IgnoreCase);
            //去除所有的样式
            html = Regex.Replace(html, @"\sstyle=""([^"";]+;?)+""", "", RegexOptions.IgnoreCase);

            //去除所有的html标签
            html = Regex.Replace(html, @"<[^>]*>|&nbsp;", "", RegexOptions.IgnoreCase);
            return html;
        }
时间: 2024-08-27 11:48:36

过滤字符串中的html标签的相关文章

java-正则表达式过滤字符串中的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> * * @a

js去除字符串中所有html标签及&amp;nbsp符号

近日在做项目的时候,经常会在页面上处理一些数据.结果发现自己js掌握的并不是很好.那就在这里记录js的点点滴滴吧. 1. 去除字符串中的 html 标签 function delHtmlTag(str){ return str.replace(/<[^>]+>/g,""); } var str = "<span style='display:none;'>This is test</span><br/>"; st

php去除字符串中的HTML标签

php自带的函数可以去除/删除字符串中的HTML标签/代码. strip_tags(string,allow):函数剥去 HTML.XML 以及 PHP 的标签. 参数:string,必填,规定要检查的字符串:allow,选填,规定允许存在的标签,这些标签不会被删除. <?php $str = '不嫌不闲-<span style="color:#f00;">PHP</span>'; $str1 = strip_tags($str); // 删除所有HTML

c# 过滤字符串中的重复字符

有字符串"a,s,d,v,a,v",如果想去除其中重复的字符,怎么做? 下面是一个方法,用Hashtable来记录唯一字符,排除重复字符,仅供参考. 1.过滤方法: public class OperationString { /// <summary> /// 过滤字符串中的重复字符 /// </summary> /// <param name="str">要过滤的字符串</param> /// <return

[.net]利用正则表达式过滤字符串中的HTML代码

01.<span style="white-space: pre;"> </span>/// <summary> 02. /// 过滤字符串中的html代码 03. /// </summary> 04. /// <param name="Str">传入字符串</param> 05. /// <returns>过滤后的字符串</returns> 06. public sta

《Python CookBook2》 第一章 文本 - 过滤字符串中不属于指定集合的字符 &amp;&amp; 检查一个字符串是文本还是二进制

过滤字符串中不属于指定集合的字符 任务: 给定一个需要保留的字符串的集合,构建一个过滤函数,并可将其应用于任何字符串s,函数返回一个s的拷贝,该拷贝只包含指定字符集合中的元素. 解决方案: import string allchars = string.maketrans('','') #all chars table def makefilter(keep): delchars = allchars.translate(allchars,keep) def thefilter(s): retu

正则匹配去掉字符串中的html标签

1.得到超链接中的链接地址: string matchString = @"<a[^>]+href=\s*(?:'(?<href>[^']+)'|""(?<href>[^""]+)""|(?<href>[^>\s]+))\s*[^>]*>"; 2.得到title标签中的值: string matchString = @"<title>(?&

过滤字符串中中文

1 USE [master]  2 GO 3 /****** Object:  UserDefinedFunction [dbo].[RemoveCharRep]    Script Date: 04/06/2016 17:34:54 ******/ 4 SET ANSI_NULLS ON 5 GO 6 SET QUOTED_IDENTIFIER ON 7 GO 8 --============================== 9 --过滤掉字符串中中文字符 @s 为输入字符串10 --==

Python字符串处理:过滤字符串中的英文与符号,保留汉字

使用Python 的re模块,re模块提供了re.sub用于替换字符串中的匹配项. 1 re.sub(pattern, repl, string, count=0) 参数说明: pattern:正则重的模式字符串 repl:被拿来替换的字符串 string:要被用于替换的原始字符串 count:模式匹配后替换的最大次数,省略则默认为0,表示替换所有的匹配 例如 import re str = "hello,world!!%[545]你好234世界..." str = re.sub(&q