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?
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.
Pay attention to two points: (1)
*?
为非贪婪模式。(2)g
Global matching.