java过滤掉字符串中的html标签、style标签、script标签
2016-11-09 15:13
656 查看
import java.util.regex.Matcher; import java.util.regex.Pattern; public class HTMLSpirit{ public static String delHTMLTag(String htmlStr){ String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式 String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式 Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); Matcher m_script=p_script.matcher(htmlStr); htmlStr=m_script.replaceAll(""); //过滤 Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); Matcher m_style=p_style.matcher(htmlStr); htmlStr=m_style.replaceAll(""); //过滤style标签 Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); Matcher m_html=p_html.matcher(htmlStr); htmlStr=m_html.replaceAll(""); //过滤html标签 return htmlStr.trim(); //返回文本字符串 } public static String delSpace(String htmlStr){ htmlStr = htmlStr.replaceAll("\n", ""); htmlStr = htmlStr.replaceAll("\t", ""); return htmlStr.trim(); //返回文本字符串 } }
相关文章推荐
- Java过滤掉字符串中的html标签、style标签、script标签
- Java过滤任意(script,html,style)标签符,返回纯文本--封装类
- Java 过滤 html、script、style 代码得到纯字符串 方法
- Java 过滤 html、script、style 代码得到纯字符串 方法
- Java过滤任意(script,html,style)标签符,返回纯文本--封装类
- Java过滤任意(script,html,style)标签符,返回纯文本--封装类
- Java过滤任意(script,html,style)标签符,返回纯文本--封装类
- Java过滤任意(script,html,style)标签符,返回纯文本--封装类
- java使用正则表达式过滤掉字符串中的html标签
- java后台过滤字符串中HTML标签和空格
- java-正则表达式过滤字符串中的html标签
- java+Jsoup 正则过滤html网页标签【多线程数据采集之二】
- java正则表达式过滤html标签
- java 去html标签,去除字符串中的空格,回车,换行符,制表符
- C# 过滤空格、换行、script、style等等标签
- Java实现正则匹配出script标签中字符串包含某个几个单词的所有行,以及编码转换
- java过滤html标签获取纯文本信息
- java 过滤html标签
- JAVA过滤标签实现将html内容转换为文本的方法示例