js 正则表达式
js 正则去除一段html里 img标签里的style,添加图片自适应 max-width:100%
convert () {
let htmlText = '<img style="border: 0px; width: 537px; display: block;">'
let str = htmlText.replace(/<img[^>]*>/gi, function (match, capture) {
// return match.replace(/(<img[^>]*)(\/?>)/gi, "$1width='100%' $2") // 添加width="100%"
return match.replace(/style\s*?=\s*?([‘"])[\s\S]*?\1/ig, 'style="max-width:100%;height:auto;"') // 替换style
})
return str
},
为img添加style
goodinfo.Body = goodinfo.Body.replace(/style\s*?=\s*?([‘"])[\s\S]*?\1/ig, ' ') // 替换style
goodinfo.Body = goodinfo.Body.replace(/width\s*?=\s*?([‘"])[\s\S]*?\1/ig, ' ') // 替换style
goodinfo.Body = goodinfo.Body.replace(/\<img/gi, '<img style="max-width:100%;height:auto" ');
去除html中<>标签
MianTitle.replace(/<\/?.+?>/g, "")
感谢分享