function addPrefixToImg(html, prefix) { const regexp = /]+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);`