Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

正则如何去除文本中<img里的指定样式?

因为是小程序,文章中的文本都是富文本编辑器编辑而来的,就会出现很多IMG自带样式的问题,导致图片超出画面,请问该如何去除这些自带样式?

<img style="width:100%;height:auto;display:block" class="rich_pages" "0.66625" data-s="300,640" data-type="jpeg" "800" src="http://yourname.com/20200723113235722.jpg" style="box-sizing:border-box !important;overflow-wrap:break-word !important;visibility:visible !important;width:677px !important;" />

只去除IMG标签的,其它标签自带样式不去除

以上就是自带样式的img标签,文中可能若干个,可能宽度所在位置还不一样,请问如何去除并替换成max-width: 100% !important;样式呢?还望各位大佬能够给予帮助!~非常感谢!~


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

用这个正则可以去掉一个 style,如果有多个,要多 replace 几次

s.replace(/(<img(?=s).*?)s+style=".*?"(.*?/?>)/, "$1$2");

去掉之后按你想要的,自己再加 style 就是了


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...