java移除富文本編輯的h5標籤

1.使用正則表達式過濾掉h5標籤,此處將處理方法寫在一個工具類裏

import java.util.regex.Pattern;
 /**
     * 移出富文本編輯器中的h5標籤
     * @param inputString
     * @return
     */
    public static String removeHtmlTag(String inputString) {
        if (inputString == null)
            return null;
        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 {
            //定義script的正則表達式{或<script[^>]*?>[\\s\\S]*?<\\/script>
            String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
            //定義style的正則表達式{或<style[^>]*?>[\\s\\S]*?<\\/style>
            String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
            String regEx_html = "<[^>]+>"; // 定義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) {
            e.printStackTrace();
        }
        return textStr;// 返回文本字符串
    }

使用方法:

 public static void main(String[] args) {
        String a="<p class=\\\"p\\\"><b><spanyes';font-family:宋體;font-weight:bold;font-size:12.0000pt;mso-font-kerning:0.0000pt;\\\"><font face=\\\"宋體\\\">問:</font></spanyes';font-family:宋體;font-weight:bold;font-size:12.0000pt;mso-font-kerning:0.0000pt;\\\"></b><spanyes';font-family:宋體;font-weight:normal;font-size:12.0000pt;mso-font-kerning:0.0000pt;\\\"><font face=\\\"宋體\\\">在哪兒可以看到有關醫藥費報銷的通知呀?</font><spanyes';font-family:宋體;font-weight:normal;font-size:12.0000pt;mso-font-kerning:0.0000pt;\\\"><o:p></o:p></spanyes';font-family:宋體;font-weight:normal;font-size:12.0000pt;mso-font-kerning:0.0000pt;\\\"></spanyes';font-family:宋體;font-weight:normal;font-size:12.0000pt;mso-font-kerning:0.0000pt;\\\"></p><p class=\\\"p\\\" align=\\\"justify\\\"><b><spanyes';font-family:宋體;font-weight:bold;font-size:12.0000pt;mso-font-kerning:0.0000pt;\\\"><font face=\\\"宋體\\\">答:</font></spanyes';font-family:宋體;font-weight:bold;font-size:12.0000pt;mso-font-kerning:0.0000pt;\\\"></b><spanyes';font-family:宋體;font-weight:normal;font-size:12.0000pt;mso-font-kerning:0.0000pt;\\\"><font face=\\\"宋體\\\">醫藥費報銷具體時間及地點見後勤保障處(後勤服務集團)醫療費報銷通知</font><span yes';font-family:宋體;font-weight:normal;font-size:12.0000pt;\\\"=\\\"\\\"><font face=\\\"宋體\\\">(網址</font>http://hqc.njtech.edu.cn/)</span><spanyes';font-family:宋體;font-weight:normal;font-size:12.0000pt;mso-font-kerning:0.0000pt;\\\"><font face=\\\"宋體\\\">,或者南京工業大學校醫院通知公告一欄(網址</font>http://gdyy.njtech.edu.cn/),一般在每個月下旬發佈次月報銷通知,建議大家及時登錄網站查看信息,以免錯過。<spanyes';font-family:宋體;font-weight:normal;font-size:12.0000pt;mso-font-kerning:0.0000pt;\\\"><o:p></o:p>";
        System.out.println(removeHtmlTag(a));
    }

測試結果:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章