it may be more complicated to use regularity. Add this html directly to a dom node, obtain attributes through the dom node generated by variables, and convert it into a json object


.

realized with the idea of upstairs:

4</span>
</span>

`; let div = document.createElement('div'); div.innerHTML = html; function getData(node, data) { if (!Array.isArray(data)) { throw TypeError('data is not Array'); } if (node.hasChildNodes()) { node.childNodes.forEach(element => { if (element.nodeType === 1) { // getData(element, data); // } if (element.nodeType === 3) { // const text = element.nodeValue; if (!text.match(/^\s*$/g)) { // const style = element.parentNode.style; // data.push({ text: text.trim(), color: style.color, font_size: style.fontSize, }); } } }); } } let data = []; getData(div, data); console.log(data);
Menu