Javascript RegEx:删除所有HTML但链接

时间:2011-05-15 07:37:23

标签: javascript regex

如何轻松删除文本中的所有HTML但保留所有链接?我知道如何剥离所有HTML标签,但无法弄清楚如何只保留链接...

这是我的文字:

<table border="0" cellpadding="2" cellspacing="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>

由于

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?

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