首页 > Web开发 > 详细

使用正则表达式去除html中的标签元素以及js和css脚本和样式

时间:2015-11-10 02:10:04      阅读:351      评论:0      收藏:0      [点我收藏+]
public static void main(String[] args) {
		String str = readFile(new File("D:\\workspace\\izbra_front\\WebRoot\\aa\\aa.java"));
		// 先过滤 script 标签
		String reg_tag = "<[\\s]*?#t#[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?#t#[\\s]*?>".replace("#t#", "script");
		str = Pattern.compile(reg_tag,Pattern.CASE_INSENSITIVE).matcher(str).replaceAll("");
		// 再 过滤 hmtl 标签
		reg_tag = "<[^>]+>";
		reg_tag = "<[\\s\\S]*?>";
		str = Pattern.compile(reg_tag,Pattern.CASE_INSENSITIVE).matcher(str).replaceAll("");
		str = str.replaceAll(" ", "");
		str = str.replaceAll("\n{1,}", "#");
		str = str.startsWith("#") ? str.substring(1) : str;
		str = str.endsWith("#") ? str.substring(0, str.length() - 1) : str;
		System.out.println("数量=="+str.split("#").length);
		str = str.replaceAll("#", "\n");
		System.out.println(str);
		
		
	}
	
	public static String readFile(File file){
		StringBuilder sb = new StringBuilder();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
			String line = null;
			while((line = br.readLine()) != null){
				if("添加".equals(line))
					continue;
				sb.append(line+"\n");
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return sb.toString();
	}

?

使用正则表达式去除html中的标签元素以及js和css脚本和样式

原文:http://276833190.iteye.com/blog/2255648

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!