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.
string.replace(/<img.*?>/g,'[圖片]')
Pay attention to two points: (1)*?
為非貪婪模式。(2) g
Global matching.