goksel goksel - 3 months ago 17
PHP Question

Php regex matches html tags contain a certain class name

I need a regular expression matches any tags have classname "share"

I think I'm very close. with this:

class=".*share.*"


I want it to match these:

<div class="share"></div>
<div class="sdfsd share sdfsdfsdf"></div>


But not these:

<div class="sdfsd dfdgdg" share></div>
<a class="icon-share export-to-csv-button"/>
<a class="fxac link" href="/share"/>


Please visit: https://regex101.com/r/uU6dU0/2

Answer

Use this regex:

class=([^=]*)([^(a-z|A-Z|0-9|\-|_)])share("|([^(a-z|A-Z|0-9|\-|_)]).*")

https://regex101.com/r/uU6dU0/4

Edit: This one is easier and will not match multiple tags:

class=("|"([^"]*)\s)share("|\s([^"]*)")

https://regex101.com/r/uU6dU0/5