AudiS2 Posted June 3, 2008 Share Posted June 3, 2008 Hi gang, I need a regexp that will match my target word inside a text but not contained within any html tags. For example if the word is "cars" and text is: I love cars <a href="cars">cars</a> I want to match first but not the other two. Possible? Quote Link to comment https://forums.phpfreaks.com/topic/108615-match-all-apperances-of-a-word-but-not-inside-html-tags/ Share on other sites More sharing options...
dsaba Posted June 4, 2008 Share Posted June 4, 2008 Were you mistaken in your description of your expected match? Because the 3rd 'cars' isn't really inside html tags, it is simply surrounded by them, as is everything else that is not "in an html tag" in an html document. I'd think you really mean to not match just the 2nd occurrence of 'cars'. If so, look at this thread: http://www.phpfreaks.com/forums/index.php/topic,122857.0.html Also see this pattern: http://nancywalshee03.freehostia.com/regextester/regex_tester.php?seeSaved=uo23fkfh Quote Link to comment https://forums.phpfreaks.com/topic/108615-match-all-apperances-of-a-word-but-not-inside-html-tags/#findComment-557147 Share on other sites More sharing options...
AudiS2 Posted June 4, 2008 Author Share Posted June 4, 2008 Ok dsaba, good point. Let me explain it in more detail. In my particular case I need not to match a word that is a part of a link (<a>) but for other tags (<p>cars</p>) it should match them. Thing is I will be replacing the word with a link, and I do not want to do that for words that are already a part of a link (or html tag). I hope it is now more clear what I try to achieve. Quote Link to comment https://forums.phpfreaks.com/topic/108615-match-all-apperances-of-a-word-but-not-inside-html-tags/#findComment-557244 Share on other sites More sharing options...
AudiS2 Posted June 4, 2008 Author Share Posted June 4, 2008 Ok found it in the thread: /(?!(?:[^<]+>|[^>]+<\/a>))\b(words)\b/is Thanks for the help and online regtester! Quote Link to comment https://forums.phpfreaks.com/topic/108615-match-all-apperances-of-a-word-but-not-inside-html-tags/#findComment-557251 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.