java 去掉html标签


 1 import java.util.regex.Matcher; 
 2 import java.util.regex.Pattern; 
 3 
 4 public class HTMLSpirit{ 
 5     public static String delHTMLTag(String htmlStr){ 
 6         String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式 
 7         String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 
 8         String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式 
 9          
10         Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); 
11         Matcher m_script=p_script.matcher(htmlStr); 
12         htmlStr=m_script.replaceAll(""); //过滤script标签 
13          
14         Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); 
15         Matcher m_style=p_style.matcher(htmlStr); 
16         htmlStr=m_style.replaceAll(""); //过滤style标签 
17          
18         Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); 
19         Matcher m_html=p_html.matcher(htmlStr); 
20         htmlStr=m_html.replaceAll(""); //过滤html标签 
21 
22         return htmlStr.trim(); //返回文本字符串 
23     } 
24 } 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM