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);
}
}
-
- 游戏网名排行榜(精选960个)
-
2025-05-06
-
- 艺名大全
-
2025-05-06
-
- 诗句网名800例
-
2025-05-06
-
- 微信名字女(精选800个)
-
2025-05-06
-
- 伤感的微信昵称(精选1000个)
-
2025-05-06
-
- 好听又吸引人的群名字(精选1000个)
-
2025-05-06
-
- 男生微信昵称高雅不俗的名字(精选900个)
-
2025-05-06
-
- 女生英文网名
-
2025-05-06
-
- 兄弟名字500对
-
2025-05-06
-
- 适合中年人的网名(精选900个)
-
2025-05-06
-
- 搞笑的游戏名字(精选940个)
-
2025-05-06
-
- 好听的游戏名字大全
-
2025-05-06
-
- 四个字的网名大全简单大气
-
2025-05-06
-
- 王者荣耀id名字(精选930个)
-
2025-05-06
-
- 情侣网名两个字800组
-
2025-05-06
-
- 适合女生网名
-
2025-05-06
-
- 姐妹网名500个
-
2025-05-06
-
- 个性情侣网名大全500组
-
2025-05-06
-
- 炫舞好听的情侣名字500组
-
2025-05-06
-
- 好听的微博名字(精选1000个)
-
2025-05-06