| | |
Javascript using regex to fix html tags
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 25
Reputation:
Solved Threads: 0
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:
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:
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:
I believe that will start matching when it finds
Thank you,
jmasta
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)
<span id="node1" style="blah:10px" />
<span id="node1" style="blah:10px"></span>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)
myRegexp = /<span.*(\/>)/gi;
<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
Maybe something like [untested]:
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.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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.
•
•
Join Date: Apr 2007
Posts: 25
Reputation:
Solved Threads: 0
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
Thanks again,
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,
•
•
Join Date: Apr 2007
Posts: 25
Reputation:
Solved Threads: 0
Update: I checked the response.responseText coming into the w parsers. These are the same in IE and FF. They both have the correct
So the root of the issue is the code surrounding the DOMParser. I'll look into this some more.
></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.
•
•
Join Date: Apr 2007
Posts: 25
Reputation:
Solved Threads: 0
Here's what I have discovered. The content for both Internet Explorer and Firefox are the same up until:
Following this,
Thanks,
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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,
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.
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.
•
•
Join Date: Apr 2007
Posts: 25
Reputation:
Solved Threads: 0
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,
~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 12:53 pm.
![]() |
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Post Bulk Data In AJAX request
- Next Thread: Javascript Issue (Changing background of parent element on onmouseover)
| Thread Tools | Search this Thread |
ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate array automatically browser bug calendar captchaformproblem cart checkbox child close codes createrange() cursor date debugger dependent disablefirebug dom dropdown editor element embed engine events explorer ext file firefox focus form forms frameworks getselection google gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe images internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jsp jump libcurl maps masterpage math media microsoft object onmouseoutdivproblem onreadystatechange parent paypal pdf php player position post programming progressbar prototype redirect regex runtime safari scale scriptlets scroll search security shopping size software sql text textarea unicode web website windowsxp wysiwyg \n






