实例及注释:
<html>
<body><script>
function getimgsrc(htmlstr){
var reg=/<img.+?src=('|")?([^'"]+)('|")?(?:\s+|>)/gim;
var arr = [];
while(tem=reg.exec(htmlstr)){
arr.push(
tem[2]); //
tem[2]
, 很关键,指的是reg里面第2个()
选择的内容
,
}
//
用这种方式可以选择匹配字符串的一部分
return arr;
}
var htmlStr=" <P align=center> <FONT size=3> <IMG src='/include/eWebEditor/UploadFile/200941092128708.jpg' width=636 border=0>ddd <IMG style='WIDTH: 294px; HEIGHT: 245px' src=/include/eWebEditor/UploadFile/20094109234712.jpg' width=441 height=341 border=0> </P>";
var imgs=getimgsrc(htmlStr);
document.write(imgs.join('\n'));
</script>
</body>
</html>
正则表达式文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions