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 }