[Java]去除html中的标签或者元素属性(正则表达式)

后台的数据库中某个字段是富文本框输入的 带有Html的标签 ,去掉标签后返回给前台

 

1.去掉Html 标签的代码

  //过滤html标签
    Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
    Matcher m_html = p_html.matcher(htmlStr);
    htmlStr = m_html.replaceAll("");

 

2.项目中使用:

@RequestMapping(value = "/details", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "详情页面", httpMethod = "GET", produces = "application/json")
    public AjaxResult details(@ApiParam(required = true, name="id", value="id") @RequestParam(required = true,value = "id")String id) {
        //定义HTML标签的正则表达式
        String regEx_html = "<[^>]+>";
        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        if (cn.cmodes.common.utils.StringUtils.isBlank(id)) {
            AjaxResult.error("id不能为空!");
        }
        Articleinformation articleinformation = articleinformationService.selectArticleinformationById(id);

        if (articleinformation == null) {
            Bookinformation bookinformation = bookinformationService.selectBookinformationById(id);

            if (bookinformation == null) {
                Mediaphoto mediaphoto = mediaphotoService.selectMediaphotoById(id);

                if (mediaphoto == null) {
                    Archaeological archaeological = archaeologicalService.selectArchaeologicalById(id);

                    if (archaeological == null) {
                        Scholar scholar = scholarService.selectScholarById(id);
                        if (scholar == null) {
                            Institution institution = institutionService.selectInstitutionById(id);
                            if (institution != null) {
                                return AjaxResult.success().put("result",institution);
                            } else {
                                return AjaxResult.error();
                            }
                        } else {
                            return AjaxResult.success().put("result",scholar);
                        }
                    } else {
                      //  archaeological.setContents(HtmlUtils.htmlUnescape(archaeological.getContents()));

                        Matcher m_html = p_html.matcher(archaeological.getContents());
                        archaeological.setContents(m_html.replaceAll(""));
                        return AjaxResult.success().put("result",archaeological);
                    }
                } else {
                 //   mediaphoto.setSunmmaryContents(HtmlUtils.htmlUnescape(mediaphoto.getSunmmaryContents()));
                    Matcher m_html = p_html.matcher(mediaphoto.getSunmmaryContents());
                    mediaphoto.setSunmmaryContents(m_html.replaceAll(""));
                    return AjaxResult.success().put("result",mediaphoto);
                }
            } else {
              //  bookinformation.setContent(HtmlUtils.htmlUnescape(bookinformation.getContent()));
                Matcher m_html = p_html.matcher(bookinformation.getContent());
                bookinformation.setContent(m_html.replaceAll(""));
                return AjaxResult.success().put("result",bookinformation);
            }
        } else {
          //  articleinformation.setSummaryContents(HtmlUtils.htmlUnescape(articleinformation.getSummaryContents()));
            Matcher m_html = p_html.matcher(articleinformation.getSummaryContents());
            articleinformation.setSummaryContents(m_html.replaceAll(""));
            return AjaxResult.success().put("result",articleinformation);
        }
    }

3.去掉后的数据: 就是不带html 标签的数据

 

数据库中数据:
<p>xvcb<span style="text-decoration: underline; font-size: 24px;"><em><strong>cxvbxcbxc<span style="text-decoration: underline; font-size: 24px; font-family: impact, chicago;">bvcbxdsfsdf s</span>fsdgdsfgsdgfds</strong></em></span></p><p><span style="text-decoration: underline; font-size: 24px; background-color: rgb(255, 255, 0);"><em><strong>dfsfsgdfgsdf<span style="text-decoration: underline; font-size: 24px; background-color: rgb(255, 255, 0); font-family: impact, chicago;"></span></strong></em></span></p>

去掉后的数据:
"xvcbcxvbxcbxcbvcbxdsfsdf sfsdgdsfgsdgfdsdfsfsgdfgsdf"

 

4. perfect

 

转载于:https://www.cnblogs.com/zhukaixin/p/10598733.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值