0

RegExp:
/<a href="(.*) target/gim

Test string:
<a href="/rss/images/" target="_blank"><img src="/themes/blooper/footer_rss.png" alt="RSS" title="RSS"></a>,<a href="https://t.me/blooper" target="_blank"><img src="/themes/blooper/footer_telegram.png?t=1" alt="Telegram" title="Telegram"></a>,<a href="https://discord.gg/lit" target="_blank"><img src="/themes/blooper/footer_discord.png?t=1" alt="Discord" title="Discord"></a>,<a href="http://blooper.media/"><img src="/themes/blooper/blooper.png" alt="blooper.media" title="blooper.media"></a>

Result:
<a href="/rss/images/" target="_blank"><img src="/themes/blooper/footer_rss.png" alt="RSS" title="RSS"></a>,<a href="https://t.me/blooper" target="_blank"><img src="/themes/blooper/footer_telegram.png?t=1" alt="Telegram" title="Telegram"></a>,<a href="https://discord.gg/lit" target

Expected result: <a href="/rss/images/" target

1 - What is the proper RegExp to select wanted string.
2 - Where did I go wrong? Am I having wrong assumptions about workings of the engine?

J. Doe
  • 5
  • 2
  • Perhaps you are wrong from the start [Regex and HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Steve Jan 10 '18 at 14:45

1 Answers1

0

Add ? to make the regex lazy - that means it will match the shortest string possible:

/<a href="(.*?) target/

Demo here: https://regex101.com/r/5vXsxo/1

mdatsev
  • 3,054
  • 13
  • 28
  • I wonder why it's not default, it's exactly the most logical answer I would think of. – J. Doe Jan 10 '18 at 14:38
  • You can check out the [answers here](https://stackoverflow.com/questions/2274990/why-are-regular-expressions-greedy-by-default) for more information – mdatsev Jan 10 '18 at 14:40