java如何将html过滤为纯文本
java开发中jsp页面可以嵌套很多插件就可以将html形式的文本直接转化为纯文本,但是如果你已经保存下来或者没有运用插件,这个额html形式的文本你该怎么转化为纯文本呢?有次我将公告保存了html形式的,展示是直接将这个html放在对应的区域内就可以,然而,在写接口是,另外一方需要纯文本的,而我页面没有运用这个插件,怎么做?最终得以解决,希望对需要的人有用。
html文本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | < p style="margin: auto; padding: inherit; font-stretch: normal; line-height: 1.8; font-family: tahoma, "microsoft yahei", 微软雅黑; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"> 引用< a href="http://www.divcss5.com/" style="color: rgb(0, 0, 204);">CSS</ a >文件到< a href="http://www.divcss5.com/html/" style="color: rgb(0, 0, 204);">Html</ a >方法-< strong >css引入</ strong >,< strong >css引用</ strong > </ p > < p style="margin: auto; padding: inherit; font-stretch: normal; line-height: 1.8; font-family: tahoma, "microsoft yahei", 微软雅黑; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"> 使用不同的方法来引用< a href="http://www.divcss5.com/rumen/r29.shtml" style="color: rgb(0, 0, 204);">css样式</ a >表,最终到达的效果相同,但是使用不同方法应用的< a href="http://www.divcss5.com/rumen/r72.shtml" style="color: rgb(0, 0, 204);">css文件</ a >将影响到SEO及网页打开速度效率。 </ p > < p style="margin: auto; padding: inherit; font-stretch: normal; line-height: 1.8; font-family: tahoma, "microsoft yahei", 微软雅黑; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"> html引用css方法如下< br />1、直接在< a href="http://www.divcss5.com/" style="color: rgb(0, 0, 204);">div</ a >中使用css样式制作< a href="http://www.divcss5.com/" style="color: rgb(0, 0, 204);">div+css</ a >网页< br />2、html中使用style自带式< br />3、使用@import引用外部CSS文件< br />4、使用< a href="http://www.divcss5.com/html/h64.shtml" style="color: rgb(0, 0, 204);">link</ a >引用外部CSS文件 推荐此方法 </ p > < p style="margin: auto; padding: inherit; font-stretch: normal; line-height: 1.8; font-family: tahoma, "microsoft yahei", 微软雅黑; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"> < span style="color: rgb(255, 0, 0);">扩展阅读</ span >:< a href="http://www.divcss5.com/rumen/r431.shtml" style="color: rgb(0, 0, 204);">link与import区别</ a > </ p > < p style="margin: auto; padding: inherit; font-stretch: normal; line-height: 1.8; font-family: tahoma, "microsoft yahei", 微软雅黑; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"> 接下来我们将逐个讲解html引用css方法的例子 </ p > < p > < br /> </ p > |
我们现在运用正则表达式对这个html文本进行处理,使用正则表达式可以最快速的过滤到html标签,这个方法遗留的问题就是有时候空格不清楚是用户敲的还是其他原因放上去的,在本方法中我将所有空格过去掉了,如果你不需要可以去掉那句代码,代码如下:
//将html转换为纯文本,此方法最后保留了&nbps空格,使用时注意将空格替换掉
public static String delHTMLTag(String htmlStr){
String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式
Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
Matcher m_script=p_script.matcher(htmlStr);
htmlStr=m_script.replaceAll(""); //过滤script标签
Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
Matcher m_style=p_style.matcher(htmlStr);
htmlStr=m_style.replaceAll(""); //过滤style标签
Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
Matcher m_html=p_html.matcher(htmlStr);
htmlStr=m_html.replaceAll(""); //过滤html标签
return htmlStr.trim(); //返回文本字符串
}
最后的字符串就是如下图所示这样:
1 | 引用CSS文件到Html方法-css引入,css引用使用不同的方法来引用css样式表,最终到达的效果相同,但是使用不同方法应用的css文件将影响到SEO及网页打开速度效率。html引用css方法如下 1 、直接在div中使用css样式制作div+css网页 2 、html中使用style自带式 3 、使用 @import 引用外部CSS文件 4 、使用link引用外部CSS文件推荐此方法扩展阅读:link与 import 区别接下来我们将逐个讲解html引用css方法的例子 |
简单的记录下来,表达可能不清晰希望大家不要介意!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?