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-04-01 10:21:20

    Vue extend $mount 构造器详解

    本节介绍两个 Vue.js 内置但却不常用的 API——extend 和 $mount,它们经常一起使用。不常用,是因为在业务开发中,基本没有它们的用武之地,但在独立组件开发时,在一些特定的场景它们是至关重要的。

  • 2020-04-01 15:36:52

    CSS3中的transition属性详解

    transition: property duration timing-function delay transition属性是个复合属性,她包括以下几个子属性: transition-property :规定设置过渡效果的css属性名称 transition-duration :规定完成过渡效果需要多少秒或毫秒 transition-timing-function :指定过渡函数,规定速度效果的速度曲线 transition-delay :指定开始出现的延迟时间

  • 2020-04-02 17:02:25

    vue怎么能像jquery那样获得数据

    有时候我们需要获得动态的元素,但是我们没法直接用vue语法,vue一共了当前组件的对象,我们可以避免使用document.get...之类的。