[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 ...
随机推荐
- RTX二次开发::检索 COM 类工厂中 CLSID 为 {79210E58-99EB-45D0-8890-763EFEAA143F} 的组件失败,
解决方法: 1.Interop.RTXSAPILib.dll这个是32位的 将IIS 应用程序池 启用32位应用程序 设置为true就可以了, 2.把编译好的程序选择X86平台运行,就好了 程序员的基 ...
- Qt webkitwidgets模块和webenginewidgets模块
问题 将Qt开发的程序从Qt5.5或更低的版本迁移到5.6或更高的版本时,会提示webkitwidgets是unknown module. Project ERROR: Unknown module( ...
- vim调用替换文件内容
:s/vivian/sky/ 替换当前行第一个 vivian 为 sky :s/vivian/sky/g 替换当前行所有 vivian 为 sky :n,$s/vivian/sky/ 替换 ...
- hdu Lovekey(水题)
#include<stdio.h> #include<string.h> ]; int main() { ],s2[]; int l1,l2,i,j,k; while(scan ...
- javascript和jquery比较
<h1>我的第一段 JavaScript</h1> <p>请输入数字.如果输入值不是数字,浏览器会弹出提示框.</p> <input id=&qu ...
- Yii2.0 多语言设置(高级版配置方法) - 新的方法
1.设置默认语言:在mail.php配置文件加上:'language'=>'zh_CN'; 2.多语言切换 (我这边是在site控制器里面操作的所以用的'/site/language') htm ...
- Introduction MBG
文档地址 http://www.mybatis.org/generator/configreference/xmlconfig.html 源码地址 https://github.com/mybatis ...
- 命令行执行 mvn package 和常见mvn命令
cmd 打开命令提示符, 然后打开代码所在目录,例如 d: cd d:/code 执行 mvn package Maven常用命令: 1. 创建Maven的普通java项目: mvn arc ...
- 【小梅哥FPGA进阶教程】串口发送图片数据到SRAM在TFT屏上显示
十五.串口发送图片数据到SRAM在TFT屏上显示 之前分享过rom存储图片数据在TFT屏上显示,该方法只能显示小点的图片,如果想显示TFT屏幕大小的图片上述方法rom内存大小不够.小梅哥给了个方案,利 ...
- ARM汇编中一些重要伪指令
IMPORT ,定义表示这是一个外部变量的标号,不是在本程序定义的 EXPORT ,表示本程序里面用到的变量提供给其他模块调用的. 以上两个在汇编和C语言混合编程的时候用到 ENDP 表示PRO ...