java之自动过滤提交文本中的html代码script代码
public class test {
public static String Html2Text(String inputString) {
String htmlStr = inputString; // 含html标签的字符串
String textStr = "";
Pattern p_script;
Matcher m_script;
Pattern p_style;
Matcher m_style;
Pattern p_html;
Matcher m_html;
Pattern p_html1;
Matcher m_html1;
try {
String regEx_script = "<[//s]*?script[^>]*?>[//s//S]*?<[//s]*?///[//s]*?script[//s]*?>"; // 定义script的正则表达式{或<script[^>]*?>[//s//S]*?<///script>
String regEx_style = "<[//s]*?style[^>]*?>[//s//S]*?<[//s]*?///[//s]*?style[//s]*?>"; // 定义style的正则表达式{或<style[^>]*?>[//s//S]*?<///style>
String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
String regEx_html1 = "<[^>]+";
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_html1 = Pattern.compile(regEx_html1, Pattern.CASE_INSENSITIVE);
m_html1 = p_html1.matcher(htmlStr);
htmlStr = m_html1.replaceAll(""); // 过滤html标签
textStr = htmlStr;
} catch (Exception e) {
}
return textStr;// 返回文本字符串
}
public static void main(String[] args) {
String url = Html2Text("<a Style='color:red'>你是傻逼</a>");
System.out.println(url);
}
}
-
- 压抑的句子200句
-
2024-04-26
-
- 关于景物的优美句子66条
-
2024-04-26
-
- 迎接2024年的句子
-
2024-04-26
-
- 回家过年的经典句子短句31条
-
2024-04-26
-
- 关于过年的句子(通用25句)
-
2024-04-26
-
- 描写雪景唯美句子简短35句
-
2024-04-26
-
- 描写春天花的优美句子短句48条
-
2024-04-26
-
- 家长给孩子写寄语的句子(精选66句)
-
2024-04-26
-
- 有关分手的句子
-
2024-04-26
-
- 赞美党的句子合集
-
2024-04-26
-
- 形容一个人好的句子200句
-
2024-04-26
-
- 描写春节的的句子45句
-
2024-04-26
-
- 关于春节的优美的句子62条
-
2024-04-26
-
- 描写夏天的风的优美句子简短39句
-
2024-04-26
-
- 春节句子经典语句(大全49条)
-
2024-04-26
-
- 关于春节的优美句子摘抄 (通用60句)
-
2024-04-26
-
- 有关元旦的唯美句子
-
2024-04-26
-
- 爱自己的句子集锦200句
-
2024-04-26
-
- 描写雪景的唯美句子简短汇总68句
-
2024-04-26
-
- 春节员工给领导拜年的温馨句子锦集六十条
-
2024-04-26