Java清除HTML标签
2014-12-11 11:36
246 查看
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HTMLSpirit {
private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
private static final String regEx_html = "<[^>]+>"; // 定义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标签
return htmlStr.trim(); // 返回文本字符串
}
}
import java.util.regex.Pattern;
public class HTMLSpirit {
private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
private static final String regEx_html = "<[^>]+>"; // 定义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标签
return htmlStr.trim(); // 返回文本字符串
}
}
相关文章推荐
- Java清除HTML标签
- Java清除HTML标签
- Java 语言实现清除带 html 标签的内容
- Java 语言实现清除带 html 标签的内容方法
- java清除html标签
- java清除html标签
- JAVA 清除 html标签
- JAVA:清除HTML标签
- java清除字符串中的html标签
- java 清除 HTML标签格式、CSS 样式
- java清除html标签
- 【转】java使用正则表达式去除字符串的html标签
- 用C#去除字符串中HTML的格式(清除数据库中html标签)
- java使用正则表达式去除字符串的html标签
- java strip_tags 去除html标签
- JAVA清除html格式正则表达式
- java 正则表达式 过滤html标签
- Java/Js下使用正则表达式匹配嵌套Html标签
- java截取带html标签的字符串,再把标签补全(保证页面显示效果)
- Java/Js下使用正则表达式匹配嵌套Html标签