Search code examples
phpregexpreg-replacepreg-matchdomparser

regex: match string only if not part of a tag


I am trying to match a string only if it is not part of an html tag.

For example when searching for the string: "abc". <a href="foo.html">abc def</a> should match <p> foo bar foo abc foo bar</p> should match

but <a href="abc.html">foo</a> should not match.

Thanks for the help!


Solution

  • I really wouldn't use regexps to match HTML, since HTML isn't regular and there are a load of edge cases to trip you up. For all but the simplest cases I'd use an HTML parser (e.g. this one for PHP).