[JAVA]透過正則表示式過濾Html標籤

JAVA 

透過正則表示式過濾Html標籤

配合WYSIWYG時(WYSIWYG=所見其所得)

內文會參雜Html標籤

故產生需求來透過正則表示式過濾Html標籤

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

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 htmlStr = getCont();
    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標籤
    
    return htmlStr;
}