0

My goal is to replace all tags by [src] only

hello <img class="gif" src="https://media2.giphy.com/media/zU4MLqeDvGA0M/100.gif" title="1"> hello      
world <img class="gif" src="https://media2.giphy.com/media/zU4MLqeDvGA0M/100.gif" title="2"> hello2

by

[https://media2.giphy.com/media/zU4MLqeDvGA0M/100.gif] [https://media2.giphy.com/media/zU4MLqeDvGA0M/100.gif]

https://regex101.com/r/ZPOCCe/1

(My regex works fine ONLY where there is a line between the 2 images. It looks like it is greedy)

Any idea ?

yarek
  • 11,278
  • 30
  • 120
  • 219
  • Besides setting non greedy, in some cases you can also add a safeguard by replacing `.*` with one or multiple non matchers `[^"]*`. You can also use backreferences to match with like in: https://regex101.com/r/4RGCjo/1 But it's actually pretty hard, because you can have a valid src attribute without quotes and you might need something like lookaheads. – René Dec 17 '17 at 19:35
  • Instead of writing regex yourself, can't you use an actual html parser? Like the browser itself. – René Dec 17 '17 at 19:40

1 Answers1

0

You can use .*? (add a question mark after) to make the regex lazy instead of greedy (just .*).

tech4him
  • 970
  • 5
  • 20