const repairRichText = (html: string) => {
// 去掉<img>中style /width / height属性
let newContent = html.replace(/<img[^>]*>/gi, (match: any) => {
match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '')
match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '')
match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '')
match = match.replace(/style\s*=\s*(["'])(?:(?!\1).)*\1/gi, '') // 去除 style=""这样的属性
return match
})
// img标签添加style属性:max-width:100%;height:auto
newContent = newContent.replace(/\<img/gi,
'<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"'
)
return newContent;
}
<rich-text :nodes="repairRichText(item.desc)" image-menu-prevent="true"></rich-text>