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;
}


  • 2019-04-25 15:40:16

    JS对象是否拥有某属性如何判断

    原型链上继承过来的属性无法通过hasOwnProperty检测到,返回false。 需注意的是,虽然in能检测到原型链的属性,但for in通常却不行。

  • 2019-04-30 11:23:36

    elasticsearch和analysis-ik的安装使用

    全文搜索和中文分词主要介绍了两组全文搜索加中文分词方案; TNTSearch+jieba-php这套组合对于博客这类的小项目基本够用了;

  • 2019-04-30 11:42:24

    php7+laravel+coreseek(sphinx)中文搜索初步实现(Linux)

    官网www.coreseek.cn已不能下载,所以需从网上找资源, 注意的一点是,笔者安装coreseek-3.2.14版本后,使用时提示client版本高于server版本的错误, php的sphinx扩展,为使用者,为client;coreseek是系统服务,为server

  • 2019-04-30 13:55:13

    浅谈mysql fulltext全文索引优缺点

    为什么会注意到mysql的fulltext? nima, 还是上次innodb转成tokudb引擎的事,这次alter修改表引擎的时候,提示percona tokudb是不支持fulltext索引的.