[javascript学习指南]java用正则表达式删除html标签几个例子

时间:2019-08-21  来源:正则表达式  阅读:

例子1

新闻内容或者博客文章,如果显示摘要,需要去除内容的html格式标签,找到一个正则表达式,实现了:

 代码如下

/**
     * 删除input字符串中的html格式
     * 
     * @param input
     * @param length
     * @return
     */ 
    public static String splitAndFilterString(String input) { 
        if (input == null || input.trim().equals("")) { 
            return ""; 
        } 
        // 去掉所有html元素, 
        String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll( 
                "<[^>]*>", "").replaceAll("[(/>)<]", ""); 
        return str; 
    }

过滤掉所有script脚本的正则:
content.replaceAll("<script[^>]*?>[\\s\\S]*?<\\/script>", "")
过滤掉所有style的正则:
content.replaceAll("<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>", "");
过滤掉所有html标签,保留p和br标签。
content.replaceAll("]*>", "");
过滤掉所有html标签,保留p标签。
content.replaceAll("]*>", "");

例子2

 代码如下

import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class HtmlUtil {
    private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
    private static final String regEx_style = "]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
    private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
    private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符
      
    /**
     * @param htmlStr
     * @return
     *  删除Html标签
     */
    public static String delHTMLTag(String htmlStr) {
        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标签
  
        Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
        Matcher m_space = p_space.matcher(htmlStr);
        htmlStr = m_space.replaceAll(""); // 过滤空格回车标签
        return htmlStr.trim(); // 返回文本字符串
    }
      
    public static String getTextFromHtml(String htmlStr){
        htmlStr = delHTMLTag(htmlStr);
        htmlStr = htmlStr.replaceAll(" ", "");
        htmlStr = htmlStr.substring(0, htmlStr.indexOf("。")+1);
        return htmlStr;
    }
      
    public static void main(String[] args) {
        String str = " 整治“四风”   清弊除垢
公司召开党的群众路线教育实践活动动员大会
";
        System.out.println(getTextFromHtml(str));
    }
}

例子3

/

 代码如下

**

 * 删除Html标签

 *

 * @param inputString

 * @return

 */

 public static String htmlRemoveTag(String inputString) {

 if (inputString == null)

 return null;

 String htmlStr = inputString; // 含html标签的字符串

 String textStr = "";

 java.util.regex.Pattern p_script;

 java.util.regex.Matcher m_script;

 java.util.regex.Pattern p_style;

 java.util.regex.Matcher m_style;

 java.util.regex.Pattern p_html;

 java.util.regex.Matcher m_html;

 try {

 //定义script的正则表达式{或<script[^>]*?>[\s\S]*?<\/script>

 String regEx_script = "<[\s]*?script[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?script[\s]*?>";

 //定义style的正则表达式{或]*?>[\s\S]*?<\/style>

 String regEx_style = "<[\s]*?style[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?style[\s]*?>";

 String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式

 p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);

 m_script = p_script.matcher(htmlStr);

 htmlStr = m_script.replaceAll(""); // 过滤script标签

 p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);

 m_style = p_style.matcher(htmlStr);

 htmlStr = m_style.replaceAll(""); // 过滤style标签

 p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);

 m_html = p_html.matcher(htmlStr);

 htmlStr = m_html.replaceAll(""); // 过滤html标签

 textStr = htmlStr;

 } catch (Exception e) {

 e.printStackTrace();

 }

 return textStr;// 返回文本字符串

 }

[javascript学习指南]java用正则表达式删除html标签几个例子

http://m.bbyears.com/aspjiaocheng/63518.html

推荐访问:java学习路线
相关阅读 猜你喜欢
本类排行 本类最新