bella
bella
这家伙很懒,什么也没写!

注册于 2年前

回答
1
文章
1
关注者
0

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);

发布
问题