function addPrefixToImg(html, prefix) {
const regexp = /<img[^>]+src="([^">]+)"/g;
const modifiedHtml = html.replace(regexp, (match, src) => {
if (!src.startsWith(prefix)) {
return match.replace(src, `${prefix}${src}`);
}
return match;
});
return modifiedHtml;
}e.g.
const modifiedHtml = addPrefixToImg(html, prefix);
问 怎样用正则表达式,匹配字符串中所有的img标签的src属性进行批量处理?