This article describes the method of filtering out strings in Java and JavaScript in img form without displaying pictures. Share it for your reference. The specific implementation method is as follows:
1. JavaScript filters out strings in the form <img></img> and <img />
Copy the code as follows:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Filter img attribute in html</title>
<script type="text/javascript">
var htmlContent = "<div id='test'><img src='aaa' height='4' width='4'></img><img src='ff' width='44' height='444 ' /></div>";
var data = htmlContent.replace(/<img.*>.*<//img>/ig,""); //Filter image elements as <img></img>
data = data.replace(/<img.*///ig, ""); //Filter elements such as <img />
alert(data);
</script>
</head>
<body>
</body>
</html>
Program effects:
You can see that strings similar to <img></img> and <img /> have disappeared
2. Java code filters strings similar to <img></img> and <img /> in strings
Copy the code as follows: public class TestRegex {
public static void main(String[] args) {
String html = "<div id='test'><img src='aaa' height='4' width='4'></img><img src='ff' width='44' height='444 ' /></div>"; //The string to be filtered
/*
* Replace the strings in the form <img></img> and <img /> to empty strings
*/
String destStr = html.replaceAll("<img.*>.*</img>", "").replaceAll("<img.*/>", "");
System.out.println(destStr);
}
}
The output result of the program is:
<div id='test'></div>
You can see that strings similar to <img></img> and <img /> have disappeared.