js 正则表达式,匹配字符串中所有<img>(属性也要带上),替换成“${图片ID=1} id是要增长的!
2015-08-25
展开全部
<!DOCTYPE HTML>
<html>
<head>
<title>Page Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script>
onload = function (){
var html = document.body.innerHTML;
var reg = /(<img(?:(?!id|>).)*)(id[\=\"\'\s]+)?([^\"\'\s]*)([\"\']?)([^>]*>)/gi;
var idx = 0;
html = html.replace(reg, function($0, $1, $2, $3, $4, $5){
idx++;
if(typeof $2 == "undefined"){
return $1 + " id='" + idx + "'" + $3 + $4 + $5;
}
return $1 + $2 + idx + $4 + $5;
});
document.body.innerHTML = html;
}
</script>
</head>
<body>
<img src="acb.jpg" id="imgx" />
<ul>
<li><img src="sdf.png" /></li>
</ul>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询