序:解决后端传来得富文本中有混乱的部分或对图片大小进行重定
replace(x,y)
x:正则表达式,用以检索字符串中需要替换的字符
y:替换的文本
用法:
//获取到的富文本,假设为:
//<img style="float:none" src=''/><img style="float:none" src=''/>
let str = res.data.content_html
let html=str.replace(/style=",'style="width:100%;')
console.log(html)
//输出结果为:
//<img style="width:100%;float:none" src=''/><img style="float:none" src=''/>
如果需要替换多个字符串
let html = str.replace(/style="/g,'style="width:100%;')
console.log(html)
//输出结果为
//<img style="width:100%;float:none" src=''/><img style="width:100%;float:none" src=''/>
富文本字符替换——replace(x,y)
最新推荐文章于 2023-12-01 11:20:56 发布