给富文本内容里面的图片添加显示规则,使其宽度为100%,高度自动适应,并清除原有的显示规则。
/**
* 富文本内容图片优化
*/
formatRichText: function (richText) {
if (richText != null) {
let newRichText = richText.replace(/<img[^>]*>/gi, function (match, capture) {
match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
return match;
});
newRichText = newRichText.replace(/style="[^"]+"/gi, function (match, capture) {
match = match.replace(/width:[^;]+;/gi, 'width:100%;').replace(/width:[^;]+;/gi, 'width:100%;');
return match;
});
// newRichText = newRichText.replace(/<br[^>]*\/>/gi, '');
// newRichText = newRichText.str_replace(/\<img/gi, '< img style="width:100%"');
newRichText = newRichText.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"');
return newRichText;
} else {
return null;
}
},
网友评论