替换字符串中图片的src

replaceImgSrc(content)
console.log(replaceImgSrc(content))

function replaceImgSrc(str) {
  var newStr = str.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function(match, capture) {
    if(!/http|https/.test(capture)) {
      return '<img src="http://app.hsrtv.cn' + capture +'" alt="" />'
    } else {
      return '<img src="'+capture+'" alt="" />';
    }
  })
  return newStr;
}

 

posted @ 2020-12-11 16:43  ladybug7  阅读(244)  评论(0编辑  收藏  举报