javascript - Regular expression to replace img image tag with text [image]?
某草草
某草草 2017-05-19 10:26:58
0
4
780

Original content:

<p class="demo1">
    11
    <img src="images/IconfollowQuestion2.png" alt="" >
    22
    <img src="images/banner.png" style="width:250px;">
    33
</p>

The result to be obtained

11[图片]22[图片]33

How to write such a regular expression using js?

某草草
某草草

reply all(4)
大家讲道理

string.replace(/<img(.*?)>/g, "[image]")

I just wrote one randomly, I don’t know if it’s right or not

Added a ❓ to change the regular expression into lazy mode, which will match as little as possible and only match one img at a time.

If ❓ is not added, it is greedy mode, which will match as many as possible and directly match all img.

给我你的怀抱
str.replace(/<img[^>]*>/g, "[图片]"); 
滿天的星座
string.replace(/<img.*?>/g,'[图片]')

Pay attention to two points: (1)*? 为非贪婪模式。(2) g Global matching.

某草草
str.replace(/<img[^>]*>/g, "[图片]");
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!