Hector Barbossa
Hector Barbossa

Reputation: 5528

Javascript regex to replace HTML FONT Tag

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

Answers (2)

Alex Turpin
Alex Turpin

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);

http://jsfiddle.net/hhRYX/

Upvotes: 1

Manse
Manse

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

Related Questions