惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
IT之家
IT之家
月光博客
月光博客
U
Unit 42
K
Kaspersky official blog
T
Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Schneier on Security
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
C
Cisco Blogs
Spread Privacy
Spread Privacy
F
Full Disclosure
博客园 - 聂微东
T
The Blog of Author Tim Ferriss

博客园 - Voodoo's天空

时间、事件、实践 投票作弊程序制作思路(续)——突破IP限制投票 投票作弊程序制作思路 迁移SharePoint Portal Server 2003 (sps2003) 需要注意和出现的问题 一年没有更新自己的BLOG了,主要是记录一些从sqlserver导数据到oracle的解决方法 2004年的最后一个POST 开发自定义控件的笔记 (2) 开发自定义控件的笔记(1) 利用ADO中记录集筛选 Smart Client 高级开发 图解Windows历史 非常好的思想 (Design your web pages freely at runtime - by Jasper) 一个PHP版本的数据库操作类,针对MYSQL的 在不同语言中关于CheckBox的处理办法(ASP、JSP、PHP) 一个不完整的OLEDB操作类(自己乱写的,呵呵) Winform的登录窗体设计思路 在应用程序中打开浏览器 周末的中日之战…… 关于值类型和引用类型比较的问题
C#中使用正则表达式清除javascript脚本的方法
Voodoo's天空 · 2006-06-16 · via 博客园 - Voodoo's天空

最近在做一个项目,里面有个需求是分析网页文件,并且检索到需要的内容,并且显示到客户端上。

问题就出现了,如果一个网页文件中包含了<script>alert()</script>。这样的脚本文件,则会出问题了。

于是我想了很多办法,但是都不是很好用。

我参考了dudu的替换脚本的方法

public static string FilterScript(string content)

 
{

              
string regexstr=@"<script.*</script>";

              
return Regex.Replace(content,regexstr,string.Empty,RegexOptions.IgnoreCase);

 }

但是这样有问题,也就是只能替换<script></script>在一行的情况

于是修改了以上的代码成为

public static string FilterScript(string content)

         
{

              
string regexstr=@"(?i)<script([^>])*>(\w|\W)*</script([^>])*>";

              
return Regex.Replace(content,regexstr,string.Empty,RegexOptions.IgnoreCase);

          }

这样表面看问题好像解决了,但是依旧有问题。他会把只要是在<script></script>中的任何内容都替换为空,也就是说,如果出现了,<script></script>内容<script></script>的情况,会被替换为空。

于是最终修改为

        private string FilterScript(string content)
        
{
            
string regexstr=@"<script[^>]*>([\s\S](?!<script))*?</script>";
            
return Regex.Replace(content,regexstr,string.Empty,RegexOptions.IgnoreCase);

        }

这样,就会循环的进行替换,只把<script></script>之间的内容替换,而不会从头替换到脚

希望对其他能搜索到这篇内容的朋友有所帮助