栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 去掉字符串中的html标签

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 去掉字符串中的html标签

public static String takeOutHtmlTab(String htmlStr) {
    // 定义script的正则表达式,去除js可以防止注入
    String scriptRegex = "]*?>[\s\S]*?<\/script>";
    // 定义style的正则表达式,去除style样式,防止css代码过多时只截取到css样式代码
    String styleRegex = "]*?>[\s\S]*?<\/style>";
    // 定义HTML标签的正则表达式,去除标签,只提取文字内容
    String htmlRegex = "<[^>]+>";
    // 定义空格,回车,换行符,制表符
    String spaceRegex = "\s*|t|r|n";
    // 过滤script标签
    htmlStr = htmlStr.replaceAll(scriptRegex, "");
    // 过滤style标签
    htmlStr = htmlStr.replaceAll(styleRegex, "");
    // 过滤html标签
    htmlStr = htmlStr.replaceAll(htmlRegex, "");
    // 过滤空格等
    htmlStr = htmlStr.replaceAll(spaceRegex, "");
    // 过滤 
    htmlStr = htmlStr.replace(" ", "");
    // 过滤 
    htmlStr = htmlStr.replace(" ", "");
    // 返回文本字符串
    htmlStr = htmlStr.trim();
    // 去除空格" "
    htmlStr = htmlStr.replaceAll(" ", "");
    return htmlStr;
  }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/860623.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号