java 过滤标签_JAVA过滤html标签的方法

下面本文章整理了几个网友常用的过滤html的例子,这些例子根据不同人要过滤不同内容而写的,希望这些例子对你会带来帮助。

例子

正则

 代码如下复制代码

re="]*>([\\s\\S](?!

例子

 代码如下复制代码

public class FilterHTMLTags {

public static String HtmlText(String inputString) {

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 {

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

String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定义style的正则表达式{或

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标签

/* 空格 ——   */

// p_html = Pattern.compile("\\ ", Pattern.CASE_INSENSITIVE);

m_html = p_html.matcher(htmlStr);

htmlStr = htmlStr.replaceAll(" "," ");

textStr = htmlStr;

}catch(Exception e) {

}

return textStr;

}

}

例子

过滤URL网址,邮箱地址,html标签,JS代码,各种转义字符

 代码如下复制代码

public static final String Upset = " ";

public static String killTags(String news) {

String s = news.replaceAll("amp;", "").replaceAll("<","<").replaceAll(">", ">");

Pattern pattern = Pattern.compile("<(span)?\\sstyle.*?style>|(span)?\\sstyle=.*?>", Pattern.DOTALL);

Matcher matcher = pattern.matcher(s);

String str = matcher.replaceAll("");

Pattern pattern2 = Pattern.compile("(<[^>] >)",Pattern.DOTALL);

Matcher matcher2 = pattern2.matcher(str);

String strhttp = matcher2.replaceAll(" ");

String regEx = "(((http|https|ftp)(\\s)*((\\:)|:))(\\s)*(//|//)(\\s)*)?"

"([\\sa-zA-Z0-9(\\.|.)(\\s)*\\-] ((\\:)|(:)[\\sa-zA-Z0-9(\\.|.)&%\\$\\-] )*@(\\s)*)?"

"("

"(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])"

"(\\.|.)(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)"

"(\\.|.)(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)"

"(\\.|.)(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])"

"|([\\sa-zA-Z0-9\\-] (\\.|.)(\\s)*)*[\\sa-zA-Z0-9\\-] (\\.|.)(\\s)*[\\sa-zA-Z]*"

")"

"((\\s)*(\\:)|(:)(\\s)*[0-9] )?"

"(/(\\s)*[^/][\\sa-zA-Z0-9\\.\\,\\?\\'\\\\/\\ &%\\$\\=~_\\-@]*)*";

Pattern p1 = Pattern.compile(regEx,Pattern.DOTALL);

Matcher matchhttp = p1.matcher(strhttp);

String strnew = matchhttp.replaceAll("").replaceAll("(if[\\s]*\\(|else|elseif[\\s]*\\().*?;", " ");

Pattern patterncomma = Pattern.compile("(&[^;] ;)",Pattern.DOTALL);

Matcher matchercomma = patterncomma.matcher(strnew);

String strout = matchercomma.replaceAll(" ");

String answer = strout.replaceAll("[\\pP‘’“”]", " ")

.replaceAll("\r", " ").replaceAll("\n", " ")

.replaceAll("\\s", " ").replaceAll(Upset, "");

return answer;

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值