简体   繁体   中英

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 ?

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM