小程序富文本图片自适应代码片段

/ 0评 / 0
function richImage(content){
	var newStr = content.replace(/class="[\s\S]*?"/g, "")
		.replace(/<img([\s\w"-=\/\.:;]+)((?:(height="[^"]+")))/ig, '<img$1')
		.replace(/<img([\s\w"-=\/\.:;]+)((?:(width="[^"]+")))/ig, '<img$1')
		.replace(/<img([\s\w"-=\/\.:;]+)((?:(style="[^"]+")))/ig, '<img$1')
		.replace(/<img([\s\w"-=\/\.:;]+)((?:(alt="[^"]+")))/ig, '<img$1')
		.replace(/<img([\s\w"-=\/\.:;]+)/ig, '<img style="max-width: 100%;height:auto" $1 ');
	return newStr;
}
如有错误,欢迎留言纠正!

发表评论

您的电子邮箱地址不会被公开。