---
title: "java过滤html标签函数 转自：http://lbs.iteye.com/blog/208056"
author: "liupengjun1"
date: "2014-04-22T18:00:34Z"
tags: []
categories: ["互联网"]
slug: "java-html-http-lbs-iteye-com-blog-208056"
draft: false
lastmod: "2014-04-22T18:00:34Z"
id: 2052789
url: "https://liupengjun1.iteye.com/blog/2052789"
---

转自：[http://lbs.iteye.com/blog/208056](http://lbs.iteye.com/blog/208056)

 

public static String Html2Text(String inputString) {   
            String htmlStr = inputString; //含html标签的字符串   
                String textStr ="";   
          java.util.regex.Pattern p\_script;   
          java.util.regex.Matcher m\_script;   
          java.util.regex.Pattern p\_style;   
          java.util.regex.Matcher m\_style;   
          java.util.regex.Pattern p\_html;   
          java.util.regex.Matcher m\_html;   
         
          try {   
           String regEx\_script = "&lt;\[\\\\s]\*?script\[^&gt;]\*?&gt;\[\\\\s\\\\S]\*?&lt;\[\\\\s]\*?\\\\/\[\\\\s]\*?script\[\\\\s]\*?&gt;"; //定义script的正则表达式{或&lt;script\[^&gt;]\*?&gt;\[\\\\s\\\\S]\*?&lt;\\\\/script&gt; }   
           String regEx\_style = "&lt;\[\\\\s]\*?style\[^&gt;]\*?&gt;\[\\\\s\\\\S]\*?&lt;\[\\\\s]\*?\\\\/\[\\\\s]\*?style\[\\\\s]\*?&gt;"; //定义style的正则表达式{或&lt;style\[^&gt;]\*?&gt;\[\\\\s\\\\S]\*?&lt;\\\\/style&gt; }   
              String regEx\_html = "&lt;\[^&gt;]+&gt;"; //定义HTML标签的正则表达式   
            
              p\_script = Pattern.compile(regEx\_script,Pattern.CASE\_INSENSITIVE);   
              m\_script = p\_script.matcher(htmlStr);   
              htmlStr = m\_script.replaceAll(""); //过滤script标签 

              p\_style = Pattern.compile(regEx\_style,Pattern.CASE\_INSENSITIVE);   
              m\_style = p\_style.matcher(htmlStr);   
              htmlStr = m\_style.replaceAll(""); //过滤style标签   
            
              p\_html = Pattern.compile(regEx\_html,Pattern.CASE\_INSENSITIVE);   
              m\_html = p\_html.matcher(htmlStr);   
              htmlStr = m\_html.replaceAll(""); //过滤html标签   
            
           textStr = htmlStr;   
            
          }catch(Exception e) {   
                   System.err.println("Html2Text: " + e.getMessage());   
          }   
         
          return textStr;//返回文本字符串   
           }   

 

转自：[http://lbs.iteye.com/blog/208056](http://lbs.iteye.com/blog/208056)