正则逐个替换img标签内容

let str = `答:小明身高60厘米。<img src="url" /><img src="url" />`;
let imgReg = /<img.*?(?:>|\/>)/gi;//图片标签匹配正则
var srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i; //src匹配正则
let arr = str.match(imgReg);
for (let i = 0;i<arr.length;i++){
  str = str.replace(arr[i],`(@${i})`)
}
for (let i = 0;i<arr.length;i++){
  str = str.replace(`(@${i})`,`\\\\ \\text { ${arr[i]}}`)
}
console.log(str)

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×