<!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>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>
<body>
<div id="test">
1.<img src="1.Gif" alt="dsddfsa" />2.<img src="2.jpG" >3.<img src="3.jpg">4.<IMG SRC=4.JPG>5.<IMG SRC='5.JPG'>bs
<div id="divMsg">
</div>
<script type="text/javascript">
<!--
var re = /<img(.+?)src=""*([^\s]+?)""*(\s|>)/ig;
var s = document.getElementById("test").innerHTML;
var a = s.match(re);
alert(a.length);
for (var i=0;i<a.length ;i++ )
{
document.getElementById("divMsg").innerHTML += a[i]+a[i].replace(re,"$2")+"<br />";
}
//-->
</script>
</body>
</html>
===================
下面将取得图片地址的同时,取得图片里的 alt即提示内容,如下:
===================
<!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>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>
<body>
<div id="test">
1.<img src="1.Gif" alt="这是alt内容" />2.<img alt='这是alt内容2' src="2.jpG" >3.<img alt="bsbsxx" src="3.jpg">4.<IMG alt=abc SRC=4.JPG>5.<IMG SRC='5.JPG' alt="大家好,最后一个alt">bs
</div>
<div id="divMsg">
</div>
<script type="text/javascript">
<!--
var re = /<img([^>]+?)>/ig;
var s = document.getElementById("test").innerHTML;
var str=""
var tempImg=""
var re1=/alt\s*=\s*([^\s]+)/i
var re2=/src\s*=\s*(["'])([^"']+)\1/i
while(re.exec(s))
{
tempImg=RegExp.$1
str+=((re1.test(tempImg))?RegExp.$1:"")+","
re2.test(tempImg)
str+=((re2.test(tempImg))?RegExp.$2:"")+"\r\n"
}
alert(str)
//-->
</script>
</body>
</html>
转载于:https://www.cnblogs.com/amylis_chen/archive/2011/11/16/2251955.html