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);
}
}
-
- 最有诗意的微信名字(精选350个)
-
2025-05-22
-
- 带心字的微信网名大全
-
2025-05-22
-
- 好听的家族名字300个
-
2025-05-22
-
- 文雅有深意的网名385个
-
2025-05-22
-
- 5个字网名有诗意300个
-
2025-05-22
-
- 浪漫的名字300个
-
2025-05-22
-
- 好听又难忘的网名350个
-
2025-05-22
-
- 个性微信网名女300个
-
2025-05-22
-
- 霸道个性网名300个
-
2025-05-22
-
- 网名男生霸气冷酷300个
-
2025-05-22
-
- 好听网名男(精选300个)
-
2025-05-22
-
- 好看的符号网名
-
2025-05-22
-
- 好看的家族名300个
-
2025-05-22
-
- 二字微信昵称500个
-
2025-05-22
-
- 非主流网名超拽霸气女280个
-
2025-05-22
-
- 关于寂寞的网名300个
-
2025-05-22
-
- 梦幻诛仙网名300个
-
2025-05-22
-
- 大气的游戏名字330个
-
2025-05-22
-
- 好听空间名300个
-
2025-05-22
-
- qq网名女生唯美伤感300个
-
2025-05-22