RSS Forums RSS

Javascript using regex to fix html tags

Please support our JavaScript / DHTML / AJAX advertiser: Programming Forums
Thread Solved
Reply
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

  #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:
<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:
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
AddThis Social Bookmark Button
Reply With Quote  
Posts: 7,398
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 439
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Javascript using regex to fix html tags

  #2  
Dec 24th, 2008
Maybe something like [untested]:
var reg = /(<span[^/>]+)\/>/gi;
var str = "<span id='d'/><div id='3'>d</div>" +
          "<span id='j' style='blah: 10px'/>";
var a = str.replace(reg, "$1><\/span>");
alert(a);

But this would mean looking away from the actual problem; the problem here is that SPAN HTML tags need to have a ending tag. What is xmlParser here? How come xmlParser returns an invalid span tag; can't it be fixed to return proper spans?

And BTW, this isn't a Firefox problem, it is actually doing what it should do. Since SPAN tags need to have content, the /> is ignored and <span id="something" /> is treated as the start of a SPAN tag.

IMO, the solution you are seeking seems more like a hack.
I don't accept change; I don't deserve to live.

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.

-- Eric Naggum RIP :-(
Reply With Quote  
Posts: 25
Reputation: jmasta is an unknown quantity at this point 
Solved Threads: 0
jmasta jmasta is offline Offline
Light Poster

Re: Javascript using regex to fix html tags

  #3  
Dec 24th, 2008
Thank you for your input, ~S.O.S~. It was going to be a hack, yes. It's an internal web-app that was written for IE6, and now they wish to have it work in IE7 and Firefox. The two parsers used here are xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); and xmlDoc=new DOMParser(); . According to W3Schools, this is the way to handle parsing for IE/FF. So if these parsers handle things the same, then it's the responseText coming into them that is slightly different. I'm going with your suggestion of finding the root of the problem and trying to fix it there, instead of implementing a regex hack. I'll keep this thread updated with what I find out.

Thanks again,
Reply With Quote  
Posts: 25
Reputation: jmasta is an unknown quantity at this point 
Solved Threads: 0
jmasta jmasta is offline Offline
Light Poster

Re: Javascript using regex to fix html tags

  #4  
Dec 24th, 2008
Update: I checked the response.responseText coming into the w parsers. These are the same in IE and FF. They both have the correct ></span> ending. For some reason, the DOMParser changes that into /> . Then, innerHTML correctly strips the / off because spans need to have content.

So the root of the issue is the code surrounding the DOMParser. I'll look into this some more.
Reply With Quote  
Posts: 25
Reputation: jmasta is an unknown quantity at this point 
Solved Threads: 0
jmasta jmasta is offline Offline
Light Poster

Re: Javascript using regex to fix html tags

  #5  
Dec 29th, 2008
Here's what I have discovered. The content for both Internet Explorer and Firefox are the same up until:
 treeContent = spanNodes.item(0).xml || (new XMLSerializer()).serializeToString(spanNodes.item(0));

Following this, treeContent has stripped out the </span> and changed it to /> . I'm now looking into what the XMLSerializer does and trying to discover why it works differently than .xml. Any ideas?
Thanks,
Reply With Quote  
Posts: 7,398
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 439
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Javascript using regex to fix html tags

  #6  
Dec 29th, 2008
I need a bit more background on the requirement/problem here as in what is the source of the XML you are talking about? Is is shipped over the network using an async call in the form of AJAX or read from an external file? Also posting some sample code which others can hack on and reproduce your problem might just help in faster resolution since "works differently" is kinda difficult to pinpoint given there is no context established in the form of a code snippet.

Also, given that XMLSerializer is specific to Gecko based browsers [not a standard], reading the API or asking in the Mozilla forums seems to be your best bet.
I don't accept change; I don't deserve to live.

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.

-- Eric Naggum RIP :-(
Reply With Quote  
Posts: 25
Reputation: jmasta is an unknown quantity at this point 
Solved Threads: 0
jmasta jmasta is offline Offline
Light Poster

Re: Javascript using regex to fix html tags

  #7  
Dec 29th, 2008
Hello again,
~s.o.s~ I implemented your solution for the regex hack for now, I was told to go this route so the project can move forward until I can research the issue with the serializer. Thanks for your help and input.

I'll try and post some of the content and explain more in depth when I get a chance.

If anyone else has ever run into issues with the serializer, i'd love to hear your issues or solutions.

Thanks,
Last edited by jmasta : Dec 29th, 2008 at 11:53 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the JavaScript / DHTML / AJAX Forum
Views: 1245 | Replies: 6 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:31 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC