去除html標簽,包括script、style,以及html字符 等
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標簽的正則表達式 String regEx_blank=" |"|&|<|>";//定義html字符 |"|&|<|> Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); Matcher m_script=p_script.matcher(htmlStr); htmlStr=m_script.replaceAll(""); //過濾script標簽 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標簽 Pattern p_blank=Pattern.compile(regEx_blank,Pattern.CASE_INSENSITIVE); Matcher m_blank=p_blank.matcher(htmlStr); htmlStr=m_blank.replaceAll(""); //過濾;nbsp return htmlStr.trim(); //返回文本字符串 }
|
新聞熱點
疑難解答