/**
* 去除字符串中的HTML标签
* @param htmlStr 包含html标签的字符串
* @return
*/
public static String delHTMLTag(String htmlStr) {
//定义script的正则表达式
String regExScript = "<script[^>]*?>[\\s\\S]*?</script>";
//定义style的正则表达式
String regExStyle = "<style[^>]*?>[\\s\\S]*?</style>";
//定义HTML标签的正则表达式
String regExHtml = "<[^>]+>";
Pattern pScript = Pattern.compile(regExScript, Pattern.CASE_INSENSITIVE);
Matcher mScript = pScript.matcher(htmlStr);
//过滤script标签
htmlStr = mScript.replaceAll("");
Pattern pStyle = Pattern.compile(regExStyle, Pattern.CASE_INSENSITIVE);
Matcher mStyle = pStyle.matcher(htmlStr);
//过滤style标签
htmlStr = mStyle.replaceAll("");
Pattern pHtml = Pattern.compile(regExHtml, Pattern.CASE_INSENSITIVE);
Matcher mHtml = pHtml.matcher(htmlStr);
//过滤html标签
htmlStr = mHtml.replaceAll("");
//返回文本字符串
return htmlStr.trim();
}
参考文章:Java去掉html标签的各种姿势