Este artigo descreve o método de filtrar seqüências de caracteres em Java e JavaScript em forma IMG sem exibir imagens. Compartilhe para sua referência. O método de implementação específico é o seguinte:
1. JavaScript filtra Strings no formulário <mg> </mg> e <img />
Copie o código da seguinte forma: <! Doctype html public "-// w3c // dtd xhtml 1.0 transitória // pt" "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> filtro IMG Atributo em html </ititle>
<script type = "text/javascript">
var htmlcontent = "<div id = 'test'> <img src = 'aaa' altura = '4' width = '4'> </img> <img src = 'ff' width = '44 'altura =' 444 ' /> </div> ";
var dados = htmlcontent.replace (/<img.*>.*<// img>/ig, "");
data = data.replace (/<img.*/// IG, "");
alerta (dados);
</script>
</head>
<Body>
</body>
</html>
Efeitos do programa:
Você pode ver que strings semelhantes a <mg> </mg> e <img /> desapareceram
2. Filtros de código Java Strings semelhantes a <Mg> </mg> e <img /> em strings
Copie o código da seguinte forma: public class TestRegex {
public static void main (string [] args) {
String html = "<div id = 'test'> <img src = 'aaa' altura = '4' width = '4'> </img> <img src = 'ff' width = '44 'altura =' 444 ' /> </div> "; // a string a ser filtrada
/*
* Substitua as cordas no formulário <mg> </mg> e <img /> para esvaziar strings
*/
String deststr = html.replaceall ("<img.*>.*</Img>", "") .replaceall ("<img.
System.out.println (deststr);
}
}
O resultado da saída do programa é:
<div id = 'teste'> </div>
Você pode ver que as seqüências semelhantes a <mg> </mg> e <img /> desapareceram.