正则表达式匹配字符串中所有的img标签的src属性进行批量处理?-灵析社区

HAO起起

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

阅读量:1

点赞量:0

问AI