Reputation: 5528
I have a html string like <FONT id="test" face="Arial"><SPAN><P>This is some content...</P></SPAN></FONT>
. Now using javascript regex I want to remove the font tag with id test and the span tag with their associated closing tags.
So that the output becomes <P>This is some content...</P>
.How to do this ?
Upvotes: 0
Views: 1564
Reputation: 47776
It is generally agreed upon that using Regex to parse HTML is a bad idea. I would recommend you use jQuery to parse this into DOM elements and then manipulate it.
var content = $('<FONT id="test" face="Arial"><SPAN><P>This is some content...</P></SPAN></FONT>').find('span').children();
Or use (but I wouldn't recommend it) the DOM api itself:
var str = '<FONT id="test" face="Arial"><SPAN><P>This is some content...</P></SPAN></FONT>';
var div = document.createElement('div');
div.innerHTML = str;
var newStr = div.getElementsByTagName('span')[0].innerHTML;
console.log(newStr);
Upvotes: 1
Reputation: 38147
You could try this -> http://jsfiddle.net/manseuk/hAPzQ/
script :
var testdom = document.getElementById('test');
var replacedom = testdom.childNodes[0].childNodes[0];
testdom.parentNode.replaceChild(replacedom,testdom);
.replaceChild()
is cross-browser -> http://www.quirksmode.org/dom/w3c_core.html#nodemanipulation
Upvotes: 0