正则表达式过滤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 ...
随机推荐
- HTML随笔2
1. 快捷键: * div*8 + tab键 - 可快捷出现八个div标签. * 标签名."名称" + tab键 - 可快速出现类名为"名称"的该标签,如果不加 ...
- Struts2 动态调用方法
struts2动态调用方法有两种方式 方式一:用通配符进行调用: Action方法: package com.bjyinfu.struts.actions; public class CatchDyn ...
- MySQL buffer pool中的三种链
三种page.三种list.LRU控制调优 一.innodb buffer pool中的三种页 1.free page:从未用过的页 2.clean page:干净的页,数据页的数据和磁盘一致 3.d ...
- 我的"Hello World!"之旅
不知从何谈起,回想起三年前的自己,那会是高三:同时也是一个让无数骚年悸动的年代,那时候的我也在一个叫做“召唤师峡谷”神奇的地方闯荡了两年.那么,记忆又会回到2012年的元旦,伴随着几个好友,偷偷摸摸地 ...
- OC和JS的交互
1.引入类拓展UIWebView+TS_JavaScriptContext, 这个类拓展是能在JSCotext出现的时候就可以拿到. 因为一般情况下JSCotext在webViewDidFinishL ...
- akoj-1076-Encoding
Encoding Time Limit:1000MS Memory Limit:65536K Total Submit:62 Accepted:35 Description Given a stri ...
- com.mysql.jdbc.exceptions.MySQLSyntaxErrorException错误
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the ...
- 流畅python学习笔记:第十七章:并发处理
第十七章:并发处理 本章主要讨论Python3引入的concurrent.futures模块.在python2.7中需要用pip install futures来安装.concurrent.futur ...
- android学习SeekBar的使用
SeekBar介绍听歌的时候,我们常常想快进或者快退到某一时间段,听歌的时候我们控制音量大小听歌,SeekBar可以通过滑块的位置来标示数值,而且拖动条允许用户拖动滑块来改变进度条的大小 SeekBa ...
- Mac实用操作技巧(六)
Night Shift,保护你夜间的眼睛 之前在Mac上有一款很著名的软件叫f.lux,这款软件会根据Mac的时间和地理位置,来自动切换屏幕亮度,如果进入晚上则调成暖色,白天就切换成普通颜色.现在Ma ...