search

Home  >  Q&A  >  body text

javascript - Regular expression to replace img image tag with text [image]?

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?

某草草某草草2777 days ago814

reply all(4)I'll reply

  • 大家讲道理

    大家讲道理2017-05-19 10:28:58

    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.

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-19 10:28:58

    str.replace(/<img[^>]*>/g, "[图片]"); 

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-19 10:28:58

    string.replace(/<img.*?>/g,'[图片]')

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

    reply
    0
  • 某草草

    某草草2017-05-19 10:28:58

    str.replace(/<img[^>]*>/g, "[图片]");

    reply
    0
  • Cancelreply