格式化字符串中的style样式

//格式化富文本图片内容样式
function formatImg(htmlstr) {
// 正则匹配所有img标签
if(htmlstr == null) return
// var regex0 = new RegExp("(i?)(]+>)","gmi");
// 正则匹配不含style="" 或 style='' 的img标签
// eslint-disable-next-line no-useless-escape
var regex1 = new RegExp("(i?)(?style='"['"])[^>]+>)", 'gmi')
// 给不含style="" 或 style='' 的img标签加上style=""
htmlstr = htmlstr.replace(regex1, '3')
console.log('增加style=""后的html字符串:' + htmlstr)
// 正则匹配含有style的img标签
// eslint-disable-next-line no-useless-escape
var regex2 = new RegExp("(i?)(?style=['"])([^>]+>)", 'gmi')
// 在img标签的style里面增加css样式(这里增加的样式:display:block;max-width:100%;height:auto;border:5px solid red;)
htmlstr = htmlstr.replace(regex2, '3')
console.log('在img标签的style里面增加样式后的html字符串:' + htmlstr)
return htmlstr
}
————————————————
版权声明:本文为CSDN博主「web前端开发大全」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_35858830/java/article/details/102589188

你可能感兴趣的:(格式化字符串中的style样式)