[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 ...
随机推荐
- c语言和设计模式
在网上看到一个博客专门写了关于设计模式的文章,感觉很有用.其实,我感觉数据结构 算法 设计模式 这三样同等重要啊. 数据结构 算法相对而言接触的比较多,但设计模式这个东西真的一头雾水,有时候觉得别人写 ...
- 洛谷 P3627 [APIO2009](抢掠计划 缩点+spfa)
题目描述 Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是,Siruseri 的酒吧也都设 ...
- 1045 Bode Plot
题目链接:http://poj.org/problem?id=1045 一道数学物理题, 推理公式:http://www.cnblogs.com/rainydays/archive/2013/01/0 ...
- es学习-索引别名
别名不能重复,也不能喝索引名称重复.(一个索引可以创建多个别名) 语法: 添加一个别名: url:POST http://192.168.0.108:9200/_aliases/ 参数: { &quo ...
- [GO]runtime包及gosched的使用
Gosched:让出CPU时间片 Goexit:退出当前的协程 GOMAXPROCS:设置使用最大的CPU数量(哇,牛逼了...) package main import ( "fmt&qu ...
- [GO]并行和并发的区别
并行:指在同一时刻,有多条指令在多个处理器上同时执行 并发:指在同一时刻只能有一条指令执行,但多个进程指令被快速的轮换执行,使得在宏观上具有多个进程同时执行的效果,但在微观上并不是同时执行的,只有把时 ...
- LightOJ 1268 Unlucky Strings (KMP+矩阵快速幂)
题意:给出一个字符集和一个字符串和正整数n,问由给定字符集组成的所有长度为n的串中不以给定字符串为连续子串的有多少个? 析:n 实在是太大了,如果小的话,就可以用动态规划做了,所以只能用矩阵快速幂来做 ...
- C#记录程序运行时间
主要:using System.Diagnostics;当中有Stopwatch类: 介绍如下: // 摘要: // 提供一组方法和属性,可用于准确地测量运行时间. public class Stop ...
- webapi 跨域访问设置基于jsonp跨域
JSONP实现跨域 Web API并没有提供JSONP Formatter,但是这并不能影响我们前进的脚步,我们可以自定义Formatter来实现JSONP功能.既然是利用JSONP跨域,那么就得简 ...
- 洛谷P4234 最小差值生成树(lct动态维护最小生成树)
题目描述 给定一个标号为从 11 到 nn 的.有 mm 条边的无向图,求边权最大值与最小值的差值最小的生成树. 输入输出格式 输入格式: 第一行两个数 n, mn,m ,表示图的点和边的数量. ...