[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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM