开发者

Javascript RegEx: remove all HTML but links

Any way to easily remove all HTML from text but keep all links? I know how to strip all HTML tags, but can't figure out how to keep just links...

Here's my text:

<table border="0" cellpadding="2" cellspa开发者_Python百科cing="7" style="vertical-align:top;">
<tr>
  <td width="80" align="center" valign="top"></td>

  <td valign="top" class="j">
    <font style="font-size:85%;font-family:arial,sans-serif"><br /></font>

    <div style="padding-top:0.8em;">
      <font style="font-size:85%;font-family:arial,sans-serif"><img alt="" height="1"
      width="1" /></font>
    </div>

    <div class="lh">
      <font style="font-size:85%;font-family:arial,sans-serif"><a href=
      "http://news.google.com/news/url?sa=t&amp;fd=R&amp;usg=AFQjCNHlTH-LLUq2nHL30fKdJcA62JseSg&amp;url=http://edition.cnn.com/2011/WORLD/africa/05/13/egypt.suzanne.mubarak/">
      <b>Egypt's former first lady said to have suffered a heart attack</b></a><br />
      <font size="-1"><b><font color="#6F6F6F">CNN
      International</font></b></font><br />
      <font size="-1">"Hosni Mubarak was also questioned about his luxury mansion in
      Sharm el-Sheikh," al-Gohary <b>said</b>. Last month, the <b>former</b>
      president <b>suffered a heart attack</b> during questioning over possible
      corruption charges, <b>Egyptian</b> state television reported.
      <b>...</b></font><br />
      <br />
      <a class="p" href=
      "http://news.google.com/news/more?gl=us&amp;pz=1&amp;ned=us&amp;ncl=dmv9Cur49MVVXrM">
      <font class="p" size="-1"><nobr><b>and
      more&nbsp;&#187;</b></nobr></font></a></font>
    </div>
  </td>
</tr>

Thanks


Is this what you want?

function getLinks() {
  var table = document.getElementsByTagName('table')[0];
  var links = table.getElementsByTagName('a');
  for (var i=links.length-1;i>=0;--i) {
    table.parentNode.insertBefore(links[i],table.parentNode.firstChild)
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜