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);
}
}
-
- 你的名字经典文案 你的名字文案配图(精选46个)
-
2023-11-02
-
- 霸气新潮的群名字大全 霸气群名称大全最火(精选97个)
-
2023-11-02
-
- 你的名字最火的一句话 你的名字最火的一句话可复制(精选27个)
-
2023-11-02
-
- 好听有含义的微信名字(精选146个)
-
2023-11-02
-
- 吸引人有情义的公会名字大全(精选122个)
-
2023-11-02
-
- 和平精英名字大全霸气 好听又沙雕的吃鸡名字(精选126个)
-
2023-11-02
-
- 好听的家族名字古风仙气 古风古韵家族名字(精选125个)
-
2023-11-02
-
- 又霸气又冷酷的军团名 古代江湖神秘组织名字(精选147个)
-
2023-11-02
-
- 文艺的相册名字 给自己的相册名字独特(精选117个)
-
2023-11-02
-
- 宠物情侣名字 宠物情侣名字一对好听(精选97个)
-
2023-11-02
-
- 逗比二人组合名字 逗比二人组合名字兄弟(精选83个)
-
2023-11-02
-
- lol名字大全女2023 随机普通名字(精选147个)
-
2023-11-02
-
- 霸气的家族名字 家族名字高冷霸气好听(精选146个)
-
2023-11-02
-
- 情侣王者荣耀名字沙雕 有趣的沙雕王者荣耀昵称(精选112个)
-
2023-11-02
-
- 霸气的王者名字大全 王者昵称男霸气(精选146个)
-
2023-11-02
-
- 字独特好听名字 三个字的好名字(精选159个)
-
2023-11-02
-
- qq备注名称大全 有趣的备注名字大全(精选146个)
-
2023-11-02
-
- 和平精英个性名字 和平精英空白名字(精选147个)
-
2023-11-02
-
- 疯疯癫癫也是一种快乐 王者荣耀情侣名字搞笑逗比(精选69个)
-
2023-11-02
-
- 好听又清新的战队名字2023 字战队名字押韵点的(精选146个)
-
2023-11-02