/**
* 去除字符串中html标签
*
* @param htmlStr
* @return
*/
private static String delHTMLTag(String htmlStr) {
if (htmlStr == null || "".equals(htmlStr)) {
return "";
}
String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
// 过滤script标签
Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
Matcher m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll("");
// 过滤style标签
Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
Matcher m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll("");
// 过滤html标签
Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
Matcher m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll("");
return htmlStr;
}
java去掉字符串中的html标签
最新推荐文章于 2024-01-19 10:06:32 发布
本文介绍了一个简单的Java方法,用于从字符串中移除所有的HTML标签。该方法首先检查输入字符串是否为空或仅包含空格,如果是,则直接返回空字符串。接着通过正则表达式分别匹配并移除script、style及一般的HTML标签。
555

被折叠的 条评论
为什么被折叠?



