本文介绍了.NET取下HTML页面的JavaScript和CSS代码块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有HTML作为字符串,JavaScript和CSS代码块
I have html as string with javascript and css code blocks.
事情是这样的:
<script type="text/javascript"> alert('hello world'); </script> <style type="text/css"> A:link {text-decoration: none} A:visited {text-decoration: none} A:active {text-decoration: none} A:hover {text-decoration: underline; color: red;} </style>
但我不需要他们。我怎样才能删除与reqular表达这些块
But i dont need them. How can i remove with reqular expressions those blocks?
推荐答案快速'N'肮脏的方法将是一个正则表达式是这样的:
The quick 'n' dirty method would be a regex like this:
var regex = new Regex( "(\\<script(.+?)\\</script\\>)|(\\<style(.+?)\\</style\\>)", RegexOptions.Singleline | RegexOptions.IgnoreCase ); string ouput = regex.Replace(input, "");
更好*(但可能更慢)的选择是使用的HtmlAgilityPack :
HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(htmlInput); var nodes = doc.DocumentNode.SelectNodes("//script|//style"); foreach (var node in nodes) node.ParentNode.RemoveChild(node); string htmlOutput = doc.DocumentNode.OuterHtml;
*)对于有关为什么它是更好的讨论,请参阅此线程。
更多推荐
.NET取下HTML页面的JavaScript和CSS代码块
发布评论