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);
}
}
-
- 吸引人的微信昵称精选100个
-
2024-12-05
-
- 使用长尾关键词有什么好处?
-
2024-11-16
-
- 关键词选择有什么技巧?
-
2024-11-16
-
- 怎么提高在百度上的排名
-
2024-11-16
-
- 非洲有哪些国家和地区
-
2024-10-23
-
- 大写数字0到十零(大写一到10怎么写)
-
2024-10-23
-
- 激励自己的座右铭简短有深意
-
2024-10-23
-
- 立志格言
-
2024-10-23
-
- 生物对环境的适应和影响
-
2024-10-23
-
- 拉风的传奇行会名字大全(精选1450个)
-
2024-10-19
-
- 穿越火线的英文名精选1108个
-
2024-10-19
-
- 给女友的备注名称大全
-
2024-09-29
-
- 书店名字大全(精选900个)
-
2024-09-29
-
- 市场定位研究分析报告范文精选56篇
-
2024-09-28
-
- 结题报告课题来源范文(通用64篇)
-
2024-09-28
-
- 春节的历史由来
-
2024-09-22
-
- 篮球技巧过人4招
-
2024-09-22
-
- 舌苔厚白应该怎么办
-
2024-09-22
-
- 微信头像会不会影响第一印象
-
2024-09-22
-
- 明成祖是谁
-
2024-09-22