[英]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>
. 我有一个HTML字符串,例如<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. 现在使用javascript正则表达式,我想删除ID为test的字体标签和与之关联的结束标签的span标签。
So that the output becomes <P>This is some content...</P>
.How to do this ? 以便输出变为<P>This is some content...</P>
如何执行此操作?
It is generally agreed upon that using Regex to parse HTML is a bad idea. 通常都同意使用Regex解析HTML是一个坏主意。 I would recommend you use jQuery to parse this into DOM elements and then manipulate it. 我建议您使用jQuery将其解析为DOM元素,然后进行操作。
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: 或使用(但我不推荐)DOM API本身:
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);
You could try this -> http://jsfiddle.net/manseuk/hAPzQ/ 您可以尝试一下-> 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 .replaceChild()
是跨浏览器的-> http://www.quirksmode.org/dom/w3c_core.html#nodemanipulation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.