当前位置:首页 > iOS开发专区 > 正文内容

java去除数据中带有HTML标签和样式

6年前 (2019-04-16)iOS开发专区3043

/**
     * 清除Html标签
     *
     * @param inputString 目标字符串
     * @return 处理后的结果
     */
    public static String removeHtmlTag(String inputString) {
        if (inputString == null)
            return null;
        String htmlStr = inputString; // 含html标签的字符串
        String textStr = "";
        Pattern p_script;
        Matcher m_script;
        Pattern p_style;
        Matcher m_style;
        Pattern p_html;
        Matcher m_html;
        Pattern p_special;
        Matcher m_special;
        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]*?>";
            // 定义HTML标签的正则表达式
            String regEx_html = "<[^>]+>";
            // 定义一些特殊字符的正则表达式 如:     
            String regEx_special = "\\&[a-zA-Z]{1,10};";

            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标签
            p_special = Pattern.compile(regEx_special, Pattern.CASE_INSENSITIVE);
            m_special = p_special.matcher(htmlStr);
            htmlStr = m_special.replaceAll(""); // 过滤特殊标签
            textStr = htmlStr;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return textStr;// 返回文本字符串
    }

扫描二维码推送至手机访问。

版权声明:本文由 果签工坊 发布,如需转载请注明出处。

本文链接:https://applep12.vip/programming/IAl8Ea7deBoK6KD.html

标签: reg2013
分享给朋友:

相关文章

直接操作Window对象进行 打印 保存等操作

打印代码:<SCRIPT language="JavaScript">       function printdiv() {  ...

js直接操作access数据库

<html>    <head><titel>jstest</title></head>    &l...

重新设置 Myeclipse workspace

启动不提示 “Workspace Launcher”对话框的情况下首次启动Eclipse/MyEclipse时, 会弹出"Workspace Launcher"对话框, 提示设置Workspace路径. 设定好路径后,...

初学extjs 了解的一些基本属性

1.  Ext.Msg.alert("角色管理信息提示", "角色权限保存成功!");2.  store.baseParams.jsonString = jso...

关于extjs组件中的内存泄露问题

所有component的子类都自动的参与标准的ext组件的生命周期:创建,渲染,销毁,这些操作由Container类提供。在创建容器时,组件可以通过item配置选项添加到容器中,或者可以add方法动态地添加。所有组件都注册到了ext.com...

Struts、Spring、Hibernate方向面试题

Hibernate工作原理及为什么要用?原理:1.读取并解析配置文件2.读取并解析映射信息,创建SessionFactory3.打开Sesssion4.创建事务Transation5.持久化操作6.提交事务7.关闭Session8.关闭Se...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。