[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
[Java]去除html中的标签或者元素属性(正则表达式)的更多相关文章
- JAVA 去除实体中类型为string的属性值中的空格
前端传入的参数实体中,有时候会出现传入了一空格,导致操作失败,这时就可以利用java反射机制去除实体中类型为sting的属性值中的空格. java代码示例: package com.spyang.ut ...
- java-去除html中的标签或者元素属性(正则表达式/jsoup)
业务场景: 如一篇使用富文本编辑器编辑的新闻稿,需要在列表页面截取前200字作为摘要,此时需要去除html标签,截取真正的文本部分. /** * 删除Html标签 */public static St ...
- [转]java去除List中重复的元素
java去除List中重复的元素 如果用Set ,倘若list里边的元素不是基本数据类型而是对象, 那么请覆写Object的boolean equals(Object obj) 和int ...
- js去除字符串中的标签
var str="<p>js去除字符串中的标签</p>"; var result=str.replace(/<.*?>/ig,"&qu ...
- java 从List中随机取出一个元素
java 从List中随机取出一个元素 List<Integer> list = new ArrayList<>(); Random random = new Random() ...
- 转.HTML中img标签的src属性绝对路径问题解决办法,完全解决!
HTML中img标签的src属性绝对路径问题解决办法,完全解决 需求:有时候自己的项目img的src路径需要用到本地某文件夹下的图片,而不是直接使用项目根目录下的图片. 场景:eclipse,to ...
- .NET获取Html字符串中指定标签的指定属性的值
using System.Text; using System.Text.RegularExpressions; //以上为要用到的命名空间 /// <summary> /// 获取Htm ...
- java去除数组中重复的元素方法总结
/* * ArrayUnique.java * Version 1.0.0 * Created on 2017年12月16日 * Copyright ReYo.Cn */ package reyo.s ...
- Java jsp页面中jstl标签详解
JSLT标签库,是日常开发经常使用的,也是众多标签中性能最好的.把常用的内容,放在这里备份一份,随用随查.尽量做到不用查,就可以随手就可以写出来.这算是Java程序员的基本功吧,一定要扎实. JSTL ...
随机推荐
- 使用python管理Cisco设备-乾颐堂
今天发现一个老外使用python写的管理cisco设备的小框架tratto,可以用来批量执行命令. 下载后主要有3个文件: Systems.py 定义了一些不同设备的操作系统及其常见命令. Conne ...
- Exception (1) Understanding Exception Handling
When an exception is thrown, it cannot be ignored--there must be some kind of notification or termin ...
- (转)打造一套UI与后台并重.net通用权限管理系统
原文地址:http://www.cnblogs.com/LRBPMS/p/3425997.html 一.前言 从进行到软件开发这个行业现在已经有几年了,在整理出这个套开发框架之前自己做了不少重复造轮子 ...
- Cocos2d-x-2.2.2开发环境配置
1.安装各种软件: Android SDK Android NDK Apache Ant Python Eclipse(adt) Cygwin(可选) Java Cocos2d-x 2.系统环境变量配 ...
- Web环境中Spring的启动过程
1.spring不但可以在JavaSE环境中应用,在Web环境中也可以广泛应用,Spring在web环境中应用时,需要在应用的web.xml文件中添加如下的配置: …… <context-par ...
- Android 65536方法数限制的思考
前言 没想到,65536真的很小. 1 Unable to execute dex: method ID not in [0, 0xffff]: 65536 PS:本文只是纯探索一下这个65K的来源, ...
- Java的StringTokenizer类
StringTokenizer是java中object类的一个子类,继承自 Enumeration接口.此类允许一个应用程序进入一个令牌(tokens),而且StringTokenizer类用起来比S ...
- [LeetCode 题解]: Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- jquery库与其他库冲突的问题解决-jquery.noConflict()
在使用jQuery开发的时候,可能还会使用到其他的JS库,比如Prototype,但多库共存时可能会发生冲突:若是发生冲突后,可以通过以下几种方案进行解决: 一. jQuery库在其他库之前导入,直接 ...
- Spring Boot专题背景简介
鄙人13年毕业,不曾在圈子里写过总结,因此文笔颇不自信. 但人生永远没有太晚的开始,现在开始做些笔记,借此巩固下学到的新知识. 一些题外话: 前段时间,做个小项目,由于某些原因,使用Java来写(之前 ...