开发中遇到需要匹配htmk字符串中包含的所有img src的值,要用正则表达式去匹配出来,用在线正则测试工具测试,/<img [^>]*src=['"]([^'"]+)[^>]*>/,可以匹配到html中的src值,但是用在js上输出的结果却是:
<img src="http://static.cnblogs.com/images/logo_small.gif" alt="" width="142" height="55" />
将整个html的标签都给返回了回来,这样很麻烦,与期望的 http://static.cnblogs.com/images/logo_small.gif
并不一致。
看到一篇博客,讲到
content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function (match) {
console.log(match);
});
function中并不止有match一个参数,还有其他参数,capture
content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function (match, capture) {
console.log(capture);
});
capture输出就是我想要的结果
参考博客来源者:博客园@dudu