java去除Html所有标签、空格以及空白,style标签以及script标签

2018-02-01 15:45:21

java和php类似,根据正则进行去除,代码如下

/**
 * 替换掉HTML标签方法升级版
 */
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(); //返回文本字符串
}

/**
 * 字符串去掉所有空格和回车
 */
public static String replaceBlank(String str) {
   String dest = "";
   if (str!=null) {
      Pattern p = Pattern.compile("\\s*|\t|\r|\n");
      Matcher m = p.matcher(str);
      dest = m.replaceAll("");
   }
   return dest;
}


  • 2020-03-11 09:43:20

    JavaScript shift() 方法

    shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。

  • 2020-03-11 09:45:19

    45个优秀的vue开源项目

    在过去一年里,前端开发发展迅速,前端工程师的薪资亦是水涨船高。2019 更是热度不减,而作为近年来尤为热门的前端框架,Vue.js 自是积累了大量关注。本文将为你介绍 2019 年最值得关注的 45 个 Vue.js 开源项目——Let's go!

  • 2020-03-11 18:26:52

    Mac设置ADB

    adb在 ~/Library/Android/sdk/platform-tools文件夹内

  • 2020-03-11 19:40:56

    java.util.zip.ZipException: zip file is empty

    三、总结 出现 java.util.zip.ZipException: zip file is empty错误,表示你本地使用的jar包或者aar包可能为空,你可以检查下文件大小,如果为空,可以替换本地的jar包或者aar包为正常的jar包或者aar包,或者如果官方有相关的资源的话直接使用官方的依赖资源即可。

  • 2020-03-11 21:22:36

    Vue的组件化之notification组件/Vue.extend()

    一、把组件的内部结构写好,写成一个vue文件notification.vue。 二、全局设置组件属性。//如果后面不需要直接引入组件的方式调用,可以不用全局注册 index.js中一般写的是需要全局设置的属性。