You could use DocumentFragment to extract the desired data from <td>
elements.
For Node take a look at some helpers like this one: jsdom@npmjs
const td = [ '<td bgcolor="#2D2F34"> <font color="#999999">Name: </font><a href="site.php?page=send&sendto=Username"><font color="#999999">Username</font></a> </td>', '<td bgcolor="#2D2F34"> <font color="#999999">Crew: </font><a href="site.php?page=crewprofile&id=2120"><font color="#999999">My Crew</font></a> </td>', '<td bgcolor="#2D2F34"> <font color="#999999">Wealth: Rich</font></td>', '<td bgcolor="#2D2F34"> <font color="#999999">Rank: Hitman</td>', '<td bgcolor="#2D2F34"> <font color="#999999">Status: Alive ( </font><font color=green>Online</font><font color="#999999"> )</font></td>', '<td bgcolor="#2D2F34"> <font color="#999999">Messages sent: 3</font></td>', '<td bgcolor="#2D2F34"> <font color="#999999">Messages received: 1</font></td>' ];
const tr = document.createElement("tr");
const table = document.createElement("table");
const frag = document.createDocumentFragment(); // Minimal Document wrapper
tr.innerHTML = td.join("");
table.appendChild(tr);
frag.appendChild(table);
const data = [...frag.querySelectorAll("td")].reduce((ob, td) => {
const a = td.textContent.split(':');
ob[a[0].trim()] = a.slice(1).join(":").trim();
return ob;
}, {})
console.log( data );
PS:
!!!? in your array you had a </font><tr><td
← itshould be </font></td>', '<td
- which I fixed above (didn't had to... since it was parsed correctly). So yeah, first make sure you're getting a well formatted HTML array
at least.
It's exactly about such things that parsing HTML with regex is a bad idea. Even with the above mistake - the HTML is parsed correctly-sh - but extracting contents, strictly using regexp, would make it absolutely fail.
Using jsdom for Node - your code should look like:
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const td = ['<td bgcolor="#2D2F34"> <font color="#999999">Name: </font><a href="site.php?page=send&sendto=Username"><font color="#999999">Username</font></a> </td>', '<td bgcolor="#2D2F34"> <font color="#999999">Crew: </font><a href="site.php?page=crewprofile&id=2120"><font color="#999999">My Crew</font></a> </td>', '<td bgcolor="#2D2F34"> <font color="#999999">Wealth: Rich</font></td>', '<td bgcolor="#2D2F34"> <font color="#999999">Rank: Hitman</td>', '<td bgcolor="#2D2F34"> <font color="#999999">Status: Alive ( </font><font color=green>Online</font><font color="#999999"> )</font></td>', '<td bgcolor="#2D2F34"> <font color="#999999">Messages sent: 3</font></td>', '<td bgcolor="#2D2F34"> <font color="#999999">Messages received: 1</font></td>'];
const dom = new JSDOM(`<table><tr>${td.join("")}</tr></table>`);
const frag = dom.window.document;
const data = [...frag.querySelectorAll("td")].reduce((ob, td) => {
const a = td.textContent.split(':');
ob[a[0].trim()] = a.slice(1).join(":").trim();
return ob;
}, {});
console.log( data );