exec() 方法用于检索字符串中的正则表达式的匹配。
如果字符串中有匹配的值返回该匹配值,否则返回 null。
var html = '<p>这是一段富文本内容<img src="http://example.com/image1.jpg">,还有一个图片<img src="http://example.com/image2.jpg"></p>';
var regex = /<img.*?src="(.*?)"/g;
var match;
var srcList = [];
while (match = regex.exec(html)) {
//match返回一个数组,数组中有两个数值
//第一个是img整个标签字段,第二个是src中匹配的内容
srcList.push(match[1]);
}
console.log(srcList); // ["http://example.com/image1.jpg", "http://example.com/image2.jpg"]