过滤HTML以及CSS样式等标签

来源:互联网 发布:校园网网络拓扑图 编辑:程序博客网 时间:2024/06/04 23:26

现在在页面上要取出文章内容的一部分作为描述,比如下图所示:

红线框里的数据是该篇文章的内容的一部分,但是文章内容保存的时候会有html代码等的格式

为了显示正常需要过滤html标签,代码如下:

public String delHTMLTag(String htmlStr){         String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式         String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式                  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标签                 htmlStr=htmlStr.replace(" ","");        htmlStr=htmlStr.replaceAll("\\s*|\t|\r|\n","");        htmlStr=htmlStr.replace("“","");        htmlStr=htmlStr.replace("”","");        htmlStr=htmlStr.replaceAll(" ","");                  return htmlStr.trim(); //返回文本字符串     } 
程序中只需调用:

String content = delHTMLTag(content);




原创粉丝点击