<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">


<META content="MSHTML 5.50.4522.1801" name=GENERATOR></HEAD>
<BODY>
<DIV>
<P><FONT face=Arial size=2>I have a rather tricky problem. I need to be able to 
take any given HTML-string, parse it and split it up into smaller sections, 
depending on the tag nesting.</FONT></P>
<P><FONT face=Arial><FONT size=2>Example tag:&nbsp;&nbsp; 
<STRONG>Beforetags&lt;div first&gt;&lt;div second&gt;&lt;/div&gt;&lt;div 
third&gt;&lt;hr&gt;&lt;/div&gt;&lt;/div&gt;Aftertags</STRONG></FONT></FONT></P>
<P><FONT face=Arial size=2>Note how the tags i nested: One outer&nbsp;div with 
two consecutive div:s inside of it. Inside the second inner div, there's a 
&lt;hr&gt;. I want to match all the innermost and ignore the outer one for the 
moment.</FONT></P>
<P><FONT face=Arial size=2>The point is this: if I can match all 
the&nbsp;&nbsp;innermost tags I can remove them and then run the remaining 
string through the regexp and get out the next set of tags <STRONG>until the 
string is completely broken up into matching tags</STRONG>. Therefore I only 
need to match the innermost tags.</FONT></P>
<P><FONT face=Arial size=2>Solution as far as of now:<BR>var regexp= 
/<STRONG>&lt;(\w+)[^&gt;]*&gt;</STRONG>[^&lt;]*<STRONG>&lt;\/\1&gt;</STRONG>/ig&nbsp;&nbsp; 
(bold for clarity)</FONT></P>
<P><FONT face=Arial size=2>first bold part: <STRONG>match any beginning of 
tag</STRONG>: ------starts with "&lt;", then one or more letters followed 
by&nbsp;zero or more characters which isn't&nbsp;a "&gt;", then a 
"&gt;"-------<BR>middle part: <STRONG>any character which isn't a 
"&lt;"</STRONG><BR>finsihing part: <STRONG>matching closing tag</STRONG> 
---------start with&nbsp; "&lt;/", then the character combination which made up 
the starting tag, then a "&gt;"</FONT></P>
<P><FONT face=Arial size=2>This works fine as long as the string is made up of 
matching tag pairs, but breaks down whenever a nonclosed tag is used, in the 
example this is the &lt;hr&gt;, the expression can't match the outer&nbsp;<SPAN 
class=671062116-21052001>div </SPAN>tag, that's correct. It matches the first 
inner div, which also is correct <EM>but encounters problems when it tries to 
match the second inner div</EM>. Since there's a &lt;hr&gt; inside the div, it 
can't be matched correctly and just skips over this tag.</FONT></P>
<P><FONT face=Arial size=2>What i need:<BR>Somehow I need to replace the middle 
part of the expression with something that says "as long as I don't encounter a 
<STRONG>closing tag</STRONG>, keep on testing" instead of "as long as i don't 
encounter a <STRONG>&lt;</STRONG>, keep on &nbsp;testing."</FONT></P>
<P><FONT face=Arial size=2>I've tried something like 
<STRONG>[^(&lt;/)]*</STRONG>&nbsp; for the middle part, and then adjust the 
third part of the expression accordingly, but no good result. What I want the 
preceding regexp to say is "as long as i dont encounter a &lt;/, keep on 
testing", but I can't get the (<EM>pattern</EM>) to work like 
this:&nbsp;[^(<EM>pattern</EM>)]* (zero or more not equal to 
pattern).</FONT></P>
<P><FONT face=Arial size=2>I'm sorry about my long question but I hope someone 
has a good grasp one pattern matching to solve this one. It's critical to me and 
I would be most grateful.</FONT></P>
<P><FONT face=Arial size=2>/Cloak</FONT></P></DIV></BODY></HTML>