C#实现过滤html标签并保留a标签的方法
作者:shichen2014 时间:2023-02-06 17:30:24
本文实例展示C#实现过滤html标签,汉字间空格,制表符,并保留a标签的方法。分享给大家供大家参考之用。具体方法如下:
可以在公共类如Common中定义如下方法:
public static string ClearHtmlExceptA(string html) {
string acceptable = "a";
string stringPattern = @"</?(?(?=" + acceptable + @")notag|[a-zA-Z0-9]+)(?:\s[a-zA-Z0-9\-]+=?(?:(["",']?).*?\1?)?)*\s*/?>";
html = Regex.Replace(html, stringPattern, "");
html = Regex.Replace(html, @"[\t\n]", "", RegexOptions.IgnoreCase);
html = Regex.Replace(html, @"[\r]", "", RegexOptions.IgnoreCase);
//html = Regex.Replace(html, @"[\t\n\r\s]","",RegexOptions.IgnoreCase);
return html;
}
然后在你需要过滤的字段添加这个方法即可实现过滤功能。希望本文所述对大家的C#程序设计有所帮助。
标签:C#,过滤,html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
android串口开发入门之搭建ndk开发环境及第一个jni调用程序
2023-03-14 16:54:55
![](https://img.aspxhome.com/file/2023/3/117083_0s.png)
获取Android手机中所有短信的实现代码
2023-08-04 16:55:30
C#使用文件流读取文件的方法
2022-06-27 20:17:12
Spring Validation方法实现原理分析
2023-09-04 17:11:55
![](https://img.aspxhome.com/file/2023/7/81257_0s.jpg)
java数字转汉字工具类详解
2023-04-28 02:00:26
Android拼图游戏 玩转从基础到应用手势变化
2021-08-24 02:41:59
![](https://img.aspxhome.com/file/2023/7/95967_0s.gif)
c# 如何实现代码生成器
2023-11-13 19:23:35
![](https://img.aspxhome.com/file/2023/0/94540_0s.png)
SpringBoot使用WebSocket实现前后端交互的操作方法
2022-07-06 12:34:35
![](https://img.aspxhome.com/file/2023/9/78869_0s.png)
Java实现为Word每一页设置不同图片水印的效果
2023-01-30 07:10:04
![](https://img.aspxhome.com/file/2023/0/85830_0s.jpg)
Java实现画线、矩形、椭圆、字符串功能
2022-12-30 19:24:33
![](https://img.aspxhome.com/file/2023/0/72590_0s.jpg)
JAVA并发图解
2023-03-26 03:11:41
![](https://img.aspxhome.com/file/2023/0/91000_0s.png)
C#对Word文档的创建、插入表格、设置样式等操作实例
2021-10-24 23:13:46
java实现登录窗口
2023-11-24 18:09:31
![](https://img.aspxhome.com/file/2023/1/110611_0s.png)
Spring的初始化和XML解析的实现
2022-12-13 15:50:51
Java实现发送手机短信语音验证功能代码实例
2023-04-04 19:03:31
java实现网站微信扫码支付
2023-06-18 18:58:19
Flutter混合开发详解
2023-05-15 11:10:54
![](https://img.aspxhome.com/file/2023/6/137686_0s.png)
Android Init进程对信号的处理流程详细介绍
2022-10-25 18:03:31
java反射机制给实体类相同字段自动赋值实例
2023-11-25 19:52:29
AOP之事务管理<aop:advisor>的两种配置方式
2023-11-24 22:55:06