View Single Post
Join Date: Apr 2007
Posts: 25
Reputation: jmasta is an unknown quantity at this point 
Solved Threads: 0
jmasta jmasta is offline Offline
Light Poster

Javascript using regex to fix html tags

 
0
  #1
Dec 23rd, 2008
Hello everyone,
I ran into an issue that I could use a little help with. I need to search a large string, match certain parts of it, and replace a certain part of the certain part. The entire string is made up of spans, tables, and images, to build a tree. The string is what's returned from the xmlParser. What I need to do is like this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <span id="node1" style="blah:10px" />
There are a few of these buried within the rest of the structure. I need to search the structure, and change the above line to this:
<span id="node1" style="blah:10px"></span>
The reason for this, is when i do treePanel.innerHTML = myBigString;
in Firefox, these spans become nested because the '/' is stripped out. (I verified this by passing in a modified version where I manually changed the /> to ></span> and it worked). So what I need, is to come up with the regex to find this. I think I can get what I need to match, but I'm unsure how to go about replacing. Here's what I have to match:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. myRegexp = /<span.*(\/>)/gi;
I believe that will start matching when it finds <span and then stop when it encounters /> . The parens will save the ordered variable as $1. How do I replace that with ></span>?

Thank you,
jmasta
Reply With Quote