I want my regex to match on every single instance of a word, which has proven difficult when it comes to HTML-content with tags. My regex matches the last instance when it's after a p-tag, and then skip the other ones. Without the last p-tag everything work as expected.
REGEX:
/(?:^|\s|<(p|strong|b|i|em)(.*?)>)(thewordtomatch)(?:^|\s|\.|\,|\;|\:|\?|\!|\<)/gi
String for matching:
<p>Here is thewordtomatch. thewordtomatch is here too. thewordtomatch</p><p>thewordtomatch will however only have a full match on this last entry.</p>
The expected output is a match on every instance of "thewordtomatch". It only matches on the last one, because of the p-tag. Anyway to get this to work?
EDIT:
To be clear, the problem is that the match with the p-tag goes before a match with whitespace, or really removes those matches. if <p>thewordtomatch
wouldn't be present, the code works as expected.