tags:

views:

67

answers:

3

how can i match a text that is outside of an anchor tag ? example : "test <a>test</a>" i want to get the test outside of the a tags only.

thanks.

+4  A: 

You're getting into dangerous land here in the sense that trying to parse HTML/XML with regular expressions is not feasible in most cases. Is parsing the markup not an option?

Collin
i know :s and i can't parse the markup
Yassir
A: 

You need to use a look-ahead regex. These can be quite complex. See this page for more info about this topic: http://www.regular-expressions.info/refadv.html

For your simple example, something like this would do the trick:

/test(?=<a>test<\/a>)/

However, it will get very very complex if you need to match against more varied HTML, to the point that it may be virtually impossible to get right if you don't know how the input HTML will be formatted.

You may therefore find it better in the long run to use a DOM parser to locate text in an HTML document.

Spudley