在java中和javascript中过滤掉类似于img形式的字符串,从而不显示图片
1:javascript过滤掉<img></img>和<img />形式的字符串
<!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>过滤html中的img属性</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,""); //过滤如<img></img>形式的图片元素 data = data.replace(/<img.*\/>/ig, ""); //过滤如<img />形式的元素 alert(data); </script> </head> <body> </body> </html>
2:java代码过滤字符串中类似于<img></img>和<img />形式的字符串
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>"; //要被过滤的字符串 /* * 替换掉<img></img>和<img />的形式的字符串为空字符串 */ String destStr = html.replaceAll("<img.*>.*</img>", "").replaceAll("<img.*/>", ""); System.out.println(destStr); } }