fork download
  1. <?php
  2.  
  3. $re = '~(?:<figcaption|(?!^)\G)[^<#]*(?:(?:<(?!\/figcaption>)|#\B)[^<#]*)*\K#\w+~';
  4. $str = "<figcaption>blah # blah #hashtag1, #hashtag2</figcaption> #ee <figcaption>#ddddd";
  5. $subst = "<span class=\"highlight\">$0</span>";
  6. $result = preg_replace($re, $subst, $str);
  7. echo $result;
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
<figcaption>blah # blah <span class="highlight">#hashtag1</span>, <span class="highlight">#hashtag2</span></figcaption> #ee <figcaption><span class="highlight">#ddddd</span>