uniapp 常用正则表达式匹配img 匹配px

it2025-08-09  10

一、匹配img替换或清空(width、height)

1、替换 img 中的width / height
//匹配图片 res.content = res.content.replace(/<img[^>]*>/ig, function(match, capture) { //match 图片路径 //匹配图片中的width、height 进行处理替换 match = match.replace(/(width="(.*?)")|(height="(.*?)")/ig, function(getFirst, getSecond) { //getFirst 取出图片的width \ height let numArr = getFirst.split('='); let num = numArr[0] + '=' + Number(JSON.parse(numArr[1])) / 1.6 ; return num }); return match })
2、将img中width / height清空
match = match.replace(/(style="(.*?)")|(width="(.*?)")|(height="(.*?)")/ig, '');

二、匹配后台html模板中的font-size,将px --> rpx

res.content = res.content.replace(/font-size:\w+;?/g, function(val, group) { val = val.replace(/(\d+)px/g, function(value, groupp) { return groupp + 'rpx' }) return val })
最新回复(0)