正则表达式过滤HTML、JS、CSS
功能用途
主要是用来提取html页面内容时使用。
示例代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading;
using System.Text.RegularExpressions;
namespace HtmlRegex
{
public class BaseRegex
{
WebClient web = new WebClient();
public void DeBug(string path,int encoding,string content)
{
Encoding encods;
if (encoding == )
encods = Encoding.UTF8;
else
encods = Encoding.Default;
StreamWriter sw = new StreamWriter(path,true ,encods);
sw.WriteLine(content);
sw.Flush();
sw.Close();
}
public string getPageContent(string url, int encoding)
{
byte[] buff = web.DownloadData(url);
if (encoding == )
{
return Encoding.UTF8.GetString(buff);
}
return Encoding.Default.GetString(buff);
}
public string checkHtml(string html)
{
//过滤JS和CSS
Regex regex1 = new Regex(@"\<script.*?\>.+?\</script\>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
Regex regex2 = new Regex(@"\<style.*?\>.+?\</style\>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
Regex regex3 = new Regex(@"\<script.*?\>.*?\</script\>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
Regex regex4 = new Regex(@"\<style.*?\>.*?\</style\>", RegexOptions.Singleline | RegexOptions.IgnoreCase); Regex regex5 = new Regex(@"\<.*?\>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
Regex regex6 = new Regex(@"&\S{2,}?;", RegexOptions.Singleline | RegexOptions.IgnoreCase);
Regex regex7 = new Regex(@"\<!\-\-.+?\-\-\>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
Regex regex8 = new Regex(@"[\r\n]{2,}", RegexOptions.Singleline | RegexOptions.IgnoreCase);
//HTML标签包括自闭和标签
//Regex regex9 = new Regex(@"<(.*)(.*)>.*<\/\1>|<(.*) \/>", RegexOptions.Singleline | RegexOptions.IgnoreCase); html = regex1.Replace(html, "");
html = regex2.Replace(html, "");
html = regex3.Replace(html, "");
html = regex4.Replace(html, "");
html = regex5.Replace(html, "");
html = regex6.Replace(html, "");
html = regex7.Replace(html, "");
html = regex8.Replace(html, "");
html = html.Replace(" ", "");
return html;
}
}
}
正则表达式过滤HTML、JS、CSS的更多相关文章
- MVC打包压缩JS&CSS文件调试时过滤了一些文件
BundleTable.这个确实是比较好用,打包并压缩了CSS,使之加载时减少流量. 但是在调试的时候会疑问为何有很多JS,CSS文件无法打包,其实是因为调试时VS自动过滤了如下文件: 后台跟踪了 ...
- Fiddler显示指定host请求,以及过滤无用的css,js
第一步 右侧窗口点击filters 第二步 点击Use Fiters 第三步 第一个选项不动 no zone filter ,第二个选项选择 show only following hosts 第四步 ...
- electron之Windows下使用 html js css 开发桌面应用程序
1.atom/electron github: https://github.com/atom/electron 中文文档: https://github.com/atom/electron/tree ...
- JS/CSS缓存杀手——VS插件
背景 前些天去考科目二,感觉经历了一场不是高考却胜似高考的考试(10年前的5分之差, 还是难以释怀)! 一行八人,就我学的时间最少(4天,8人一辆车),教练都觉得我肯定还得再来一次! 靠着运气和 ...
- gulp实现打包js/css/img/html文件,并对js/css/img文件加上版本号
参考打包教程: http://www.cnblogs.com/tugenhua0707/p/4069769.html http://www.cnblogs.com/tugenhua0707/p/498 ...
- 配置springMVC之后,引入js,css等资源处理
配置了sringMVC之后,要引入js,css处理: 做法1:在<%page %>下面增加: <%@ taglib prefix="yesurl" uri=&qu ...
- springmvc js/css路径问题
①No mapping found for HTTP request with URI[/msm2/css/login2.css] in DispatcherServlet with name 'sp ...
- iOS之在webView中引入本地html,image,js,css文件的方法 - sky//////////////////////////////////////ZZZZZZZZZZZZZZZ
iOS之在webView中引入本地html,image,js,css文件的方法 2014-12-08 20:00:16CSDN-sky_2016-点击数:10292 项目需求 最近开发的项 ...
- springMVC下jsp引用外部js,css等静态资源的解决方法
直入主题. 1. web.xml对springMVC配置如下: <servlet> <description>Spring MVC配置</description> ...
- 在Sublime Text 3 中安装SublimeLinter,Node.js进行JS&CSS代码校验
转载自:http://www.wiibil.com/website/sublimelinter-jshint-csslint.html 在Sublime Text中安装SublimeLinter,No ...
随机推荐
- 【JAVASCRIPT】React学习- 与 flux 结合使用
摘要 react 学习包括几个部分: 文本渲染 JSX 语法 组件化思想 数据流 与flux 结合使用
- 解决kindeditor编辑器中使用百度地图时不能拖动坐标的问题
覆盖\plugins\baidumap文件夹下的map.html代码即可 <!doctype html><html><head> <meta http- ...
- Eclipse中安装MemoryAnalyzer插件及使用
Eclipse中安装MemoryAnalyzer插件 一.简介 Eclipse作为JAVA非常好用的一款IDE,其自带的可扩展插件非常有利于JAVA程序员的工作效率提升. MemoryAnalyzer ...
- macOS下配置scapy环境
测试需求需要用到scapy库,遂在本机配置scapy环境,但最后一直提示权限问题,可能和sip有关系. 最后在同事介绍下使用虚拟环境(virtualenv)搞定. virtualenv: Virtua ...
- 分布式事务,EventBus 解决方案:CAP【中文文档】
前言 很多同学想对CAP的机制以及用法等想有一个详细的了解,所以花了将近两周时间写了这份中文的CAP文档,对 CAP 还不知道的同学可以先看一下这篇文章. 本文档为 CAP 文献(Wiki),本文献同 ...
- python几种常见语法
NO1.type自动匹配类型 name="zhangsan"age=18 print (type(name))#type自动匹配类型 print ("name:%s,ag ...
- Unity 继承MonoBehaviour脚本 执行顺序 详解
先看结果 Awake ->OnEnable-> Start ->-> FixedUpdate-> Update -> LateUpdate ->OnGUI ...
- Mybatis源码分析-BaseExecutor
根据前文Mybatis源码分析-SqlSessionTemplate的简单分析,对于SqlSession的CURD操作都需要经过Executor接口的update/query方法,本文将分析下Base ...
- zabbix 3.2.7 (源码包)安装部署
Zabbix 3.2.7 + CentOS7 安装 环境准备: 操作系统 CentOS Linux release 7.2.1511 (Core) zabbix server 10.30.94.60 ...
- Python集合(set)类型的操作
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...