1

I am trying to create a regex to remove all links from a HTML table so that the links will not display when the table is exported to excel. I am using the line of JavaScript below which removes everything except the link text. Any suggestions on how to remove the link text as well? Thanks.

tableHTML = tableHTML.replace(/<a[^>]*>|<\/a>/g, "")
Isaac
  • 277
  • 4
  • 17

3 Answers3

1

I suggest this instead of a regex

window.onload = function() { // or onclick of your export
  var clonedTable = document.getElementById("table1"); // to do non-destructive copy
  var div = document.createElement("div");
  div.innerHTML=clonedTable.outerHTML;
  [].forEach.call(div.querySelectorAll("table a"),function(link) {
    link.parentNode.removeChild(link);
  });
  document.body.appendChild(div); // or something else
}
<table id="table1">
  <tbody>
    <tr>
      <td>Here is a link <a href="bla">Bla</a> and one more <a href="bla">Bla</a> and one more <a href="bla">Bla</a> and one more <a href="bla">Bla</a> and one more</td>
    </tr>
    <tr>
      <td>And a link <a href="bla">Bla</a> and one more <a href="bla">Bla</a></td>
    </tr>
  </tbody>
</table>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Thanks for the reply, this opens up a lot of possibilities. However, for my question I don't want to actually manipulate the DOM and change the displayed page, I just want to edit the HTML used to create an Excel document. – Isaac May 23 '16 at 13:08
  • I understand. You do not have to render the table. It is however strongly recommended not to use regex but dom where you have the power of selectors and such to cleanly remove the objects – mplungjan May 23 '16 at 13:11
  • How could I manipulate the html like this without the changes being reflected in the table on the page? – Isaac May 23 '16 at 16:14
0

Try this

<p id="mytext">Hello World</p>
<script>
    var element = document.getElementById('mytext');
    var highlightedText = element.innerHTML;
    var parent = element.parentNode;
    var newNode = document.createTextNode(highlightedText);

    parent.insertBefore(newNode, element);
    parent.removeChild(element);
</script>
keziah
  • 564
  • 1
  • 6
  • 24
-1

I'm not sure what are you trying to do, But if you want to remove all links, this pattern would help you:

/<a.*?a>/gm

Online Demo


Full Code:

tableHTML = tableHTML.replace(/<a.*?a>/gm, "")
Shafizadeh
  • 9,960
  • 12
  • 52
  • 89