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-12-24
-
- 十大好用的眼线笔排行榜,眼线笔排行榜10强前十名
-
2025-12-24
-
- 十大电子表品牌排行榜,电子手表品牌排行榜前十名
-
2025-12-24
-
- 十大带在身上辟邪物品,带什么东西辟邪最厉害
-
2025-12-24
-
- 十大菜刀品牌排行榜,中国刀具十大名牌排名
-
2025-12-24
-
- 十大补水面膜排行榜10强,超补水面膜牌子排行榜
-
2025-12-24
-
- 十大保温杯品牌排行榜,保温杯10大品牌排名
-
2025-12-24
-
- 圣诞节礼物排行榜大全,圣诞节送女朋友什么礼物好
-
2025-12-24
-
- 生日当天对大家的感谢话
-
2025-12-24
-
- 什么样子的太阳镜更适合你?最主要看脸型
-
2025-12-24
-
- 什么属相的员工旺老板,如何挑选员工
-
2025-12-24
-
- 什么是有机认证?
-
2025-12-24
-
- 什么是微晶石?
-
2025-12-24
-
- 什么是碳化硅 碳化硅的主要用途有哪些?
-
2025-12-24
-
- 什么是甲鱼蛋?看完你就明白了!
-
2025-12-24
-
- 什么是出血位?印刷排版以及设计制作前 为什么要加出血位?
-
2025-12-24
-
- 什么平台可以贷款20万,能个人贷款20万的网贷排行榜
-
2025-12-24
-
- 射击游戏排行榜手游,十大好玩的射击手游排名
-
2025-12-24
-
- 三万日元等于多少人民币多少
-
2025-12-24
-
- 腮红品牌热卖排行榜前十名,腮红什么牌子好用推荐
-
2025-12-24


