บทความนี้อธิบายวิธีการกรองสตริงใน 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> แอตทริบิวต์ตัวกรอง IMG ใน HTML </title>
<script type = "text/javascript">
var htmlContent = "<div id = 'test'> <img src = 'aaa' ความสูง = '4' width = '4'> </img> <img src = 'ff' width = '44 'ความสูง =' 444 ' /> </div> ";
var data = htmlcontent.replace (/<img.*>.*<// img>/ig, "");
data = data.replace (/<img.*/// ig, "");
การแจ้งเตือน (ข้อมูล);
</script>
</head>
<body>
</body>
</html>
ผลของโปรแกรม:
คุณจะเห็นว่าสตริงที่คล้ายกับ <img> </img> และ <img /> ได้หายไป
2. ตัวกรองรหัส java สตริงคล้ายกับ <img> </img> และ <img /> ในสตริง
คัดลอกรหัสดังต่อไปนี้: คลาสสาธารณะ testregex {
โมฆะคงที่สาธารณะหลัก (สตริง [] args) {
String html = "<div id = 'test'> <img src = 'aaa' ความสูง = '4' width = '4'> </img> <img src = 'ff' width = '44 'ความสูง =' 444 ' /> </div> "; // สตริงที่จะกรอง
-
* แทนที่สตริงในรูปแบบ <img> </img> และ <img /> เป็นสตริงที่ว่างเปล่า
-
String destStr = html.replaceall ("<img.*>.*</img>", "") .replaceall ("<img.*/>", "");
System.out.println (DestStr);
-
-
ผลลัพธ์ผลลัพธ์ของโปรแกรมคือ:
<div id = 'ทดสอบ'> </div>
คุณจะเห็นว่าสตริงที่คล้ายกับ <img> </img> และ <img /> หายไป