-1

I have below html tag in text format.

const text = '<td rowspan="3" colspan="2" class="confluenceTd">All Best <br><br></td>';

I want to extract both the text, rowspan & colspan from above i.e. All Best, 3, 2 respectivelly. Any help appreciated!!.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • You're using JavaScript so convert the string to a DOM object. Regex is silly for this. – MonkeyZeus Sep 06 '22 at 18:51
  • Does this answer your question? [How to extract text from html String](https://stackoverflow.com/questions/72573421/how-to-extract-text-from-html-string) – Suresh Sep 06 '22 at 21:23

1 Answers1

2

Create an element out of it then query for those attributes. Since we are talking about td it has no life without a table (implicit or explicit) so we wrap it inside a table to make it valid and targetable by querySelector.

const text = '<td rowspan="3" colspan="2" class="confluenceTd">All Best <br><br></td>';
var table = '<table><tr>' + text + '</tr></table>';
var div = document.createElement("div");
div.innerHTML = table;
var elem = div.querySelector("tr").firstChild
console.log(elem.getAttribute("rowspan"));
console.log(elem.getAttribute("colspan"));
console.log(elem.innerText);
WOUNDEDStevenJones
  • 5,150
  • 6
  • 41
  • 53
IT goldman
  • 14,885
  • 2
  • 14
  • 28