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);
}
}
-
- 婚庆公司取名字大全
-
2025-08-11
-
- 好听的花店名字
-
2025-08-11
-
- 适合年轻人的火锅店名
-
2025-08-11
-
- 有意境的花店名字
-
2025-08-11
-
- 高端洋气的英文店名
-
2025-08-11
-
- 好听的装修公司名字
-
2025-08-11
-
- 公司名称国风:赋予企业文化更加深远的意义
-
2025-08-11
-
- 创意海鲜店名字大全集
-
2025-08-11
-
- 餐饮公司名字大全
-
2025-08-11
-
- 火锅店取名独特一点的
-
2025-08-11
-
- 小火锅店名字大全
-
2025-08-11
-
- 杂货店起名大全
-
2025-08-11
-
- 有创意的饭店名字
-
2025-08-11
-
- 金融服务公司起名大全
-
2025-08-11
-
- 好听的玉器店名大全
-
2025-08-11
-
- 咖啡店名字大全
-
2025-08-11
-
- 有创意的文化传媒名字
-
2025-08-11
-
- 汽车公司起名大全-汽车销售公司名字
-
2025-08-11
-
- 广告公司起名大全2026
-
2025-08-11
-
- 袜子店起名大全
-
2025-08-11