0

I'm trying to make a regex that matches a word not contained in a "a" tag, I have to replace some terms with a link, without breaking links that already exist. For example, searching for the term "stackoverflow", this should match

stackoverflow is magic

But none of these two should :

Jump to <a href="stackoverflow.com">stackoverflow</a>

How can I do it ? Even if I use a HTML Parser, I still face this problem on how to apply my preg_replace on a term not contained in a "a" tag.

I've found this regex that sounded good :

(?![^<]*a>)stackoverflow

But unfortunately, this doesn't work in PHP. Thanks a lot for your help

Tinostarn
  • 91
  • 2
  • 8

1 Answers1

1

Your regex fails if there is any a> in the line.


Skip the links like this by using (*SKIP)(*F) verbs | match word.

/<a[\s>][\s\S]*?\/a>(*SKIP)(*F)|stackoverflow/i

\s matches a whitespace, [\s\S] matches any character.

  • `[\s\S]*?` is a trick only needed in Javascript, with PHP, use `.*?` with the `s` (aka `singleline`) modifier. – Casimir et Hippolyte Sep 05 '15 at 22:28
  • Thx a lot, works like a charm. I didn't know the SKIP trick (we learn everyday about regex !) – Tinostarn Sep 07 '15 at 14:04
  • Is there a way to check also in the same expression if the word is not included in a tag, ? I also have img tags, and same pb appears when I have for example – Tinostarn Sep 25 '15 at 13:32