[英]Using JavaScript, how do I transform an HTML string into an array of HTML tags and text content?
I have an HTML string such as:我有一个 HTML 字符串,例如:
<p>
<strong><em>Lorem Ipsum </em></strong>is simply dummy text of the printing <em>and</em> typesetting industry.
</p>
I want to convert this into a JavaScript array that looks like:我想将其转换为 JavaScript 数组,如下所示:
['<p>', '<strong>', '<em>', 'Lorem Ipsum ', '</em>', '</strong>', 'is simply dummy text of the printing ', '<em>', 'and', '</em>', 'typesetting industry.', '</p>']
Ie it takes the HTML string and breaks it down into an array of tags and HTML content.即它采用 HTML 字符串并将其分解为标签数组和 HTML 内容。
I have tried to use DomParser()
as per this question:我试图根据这个问题使用
DomParser()
:
const str = `<p><strong><em>Lorem Ipsum </em></strong>is simply dummy text of the printing <em>and</em> typesetting industry.</p>`;
const doc = new DOMParser().parseFromString(str, 'text/html');
const arr = [...doc.body.childNodes]
.map(child => child.outerHTML || child.textContent);
However, this simply returns:但是,这只是返回:
['<p><strong><em>Lorem Ipsum </em></strong>is simply dummy text of the printing <em>and</em> typesetting industry.</p>']
I have also tried to search for various Regex based solutions, but haven't been able to find any that break down the string exactly as I require.我还尝试搜索各种基于正则表达式的解决方案,但无法找到任何可以完全按照我的要求分解字符串的解决方案。
Any suggestions?有什么建议么?
Thanks谢谢
I'd make a recursive function to iterate over a given node and return an array of the text representation of its children:我会做一个递归 function 来迭代给定节点并返回其子节点的文本表示数组:
const str = `<p><strong><em>Lorem Ipsum </em></strong>is simply dummy text of the printing <em>and</em> typesetting industry.</p>`; const doc = new DOMParser().parseFromString(str, 'text/html'); const parseNode = node => { const output = []; for (const child of node.childNodes) { if (child.nodeType === Node.TEXT_NODE) { output.push(child.textContent); } else if (child.nodeType === Node.ELEMENT_NODE) { output.push(`<${child.tagName}>`); output.push(...parseNode(child)); output.push(`</${child.tagName}>`); } } return output; }; console.log(parseNode(doc.body));
If you need to keep attributes too, you could take the outerHTML
of the element and take the leading non-brackets:如果您还需要保留属性,则可以采用元素的
outerHTML
并采用前导非括号:
const str = `<p style="color:green"><strong><em>Lorem Ipsum </em></strong>is simply dummy text of the printing <em>and</em> typesetting industry.</p>`; const doc = new DOMParser().parseFromString(str, 'text/html'); const parseNode = node => { const output = []; for (const child of node.childNodes) { if (child.nodeType === Node.TEXT_NODE) { output.push(child.textContent); } else if (child.nodeType === Node.ELEMENT_NODE) { const attribs = child.outerHTML.match(/<\s*[^>\s]+([^>]*)/)[1]; output.push(`<${child.tagName}${attribs}>`); output.push(...parseNode(child)); output.push(`</${child.tagName}>`); } } return output; }; console.log(parseNode(doc.body));
If you need self-closing tags not to be expanded, check if the outerHTML
of an element contains </
:如果您需要不展开自闭合标签,请检查元素的
outerHTML
是否包含</
:
const str = `<p style="color:green"><input readonly value="x"/><strong><em>Lorem Ipsum </em></strong>is simply dummy text of the printing <em>and</em> typesetting industry.</p>`; const doc = new DOMParser().parseFromString(str, 'text/html'); const parseNode = node => { const output = []; for (const child of node.childNodes) { if (child.nodeType === Node.TEXT_NODE) { output.push(child.textContent); } else if (child.nodeType === Node.ELEMENT_NODE) { const attribs = child.outerHTML.match(/<\s*[^>\s]+([^>]*)/)[1]; output.push(`<${child.tagName}${attribs}>`); if (child.outerHTML.includes('</')) { // Not self closing: output.push(...parseNode(child)); output.push(`</${child.tagName}>`); } } } return output; }; console.log(parseNode(doc.body));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.