首页 > 编程语言 >微信小程序富文本内容中的图片处理

微信小程序富文本内容中的图片处理

时间:2022-10-26 18:01:21浏览次数:69  
标签:微信 newContent replace match 文本 gi 图片

后台上传图片,大小各异,为了让图片在微信小程序更好的的显示,进行了以下处理:

找到内容中的图片,去除原有的宽高属性,给图片加上的宽度最大100%,高度自适应的样式

formatRichTextImg = (html) => {
	let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
		match = match.replace(/style="[^"]+"/gi, '').replace(/style\s*?=\s*?([‘"])[\s\S]*?\1/ig, '');
		match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
		match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
		return match;
	});
	newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"');
	return newContent;
}

 参考:https://blog.csdn.net/weixin_55556204/article/details/124488693

 

标签:微信,newContent,replace,match,文本,gi,图片
From: https://www.cnblogs.com/foxing/p/16829376.html

相关文章