Jump to content

Match all apperances of a word but not inside html tags


AudiS2

Recommended Posts

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?

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.