Javascript如何将纯文本html元素拆分为数组?

问题描述:

如果我有一个包含 html 元素的字符串,那么创建兄弟 html 元素数组的有效方法是什么?

If I have a string containing html elements, what would be an efficient way of creating an array of the sibling html elements?

尝试将 indexOf 与起始标签的第一个字母"<"一起使用并且最后一个结束标记变得复杂,因为可以有子元素.

Trying to use indexOf with the first letter of the opening tag " <" and the last one of the closing tag becomes complicated since there can be child elements.

简单例子:

<p>Hello there</p>

<h1>Thank you</h1>

提前致谢!

使用 regex 和 js split 方法我们可以提取.

Using regex with js split method we can extract.

检查我的代码

let a = `<p>Hello there</p><p>How r you?</p>

<h1>Thank you</h1>`
let b = a.split(/<[a-zA-Z0-9]*>([^<.*>;]*)<\/[a-zA-Z0-9]*>/gmi).filter(x=>x.trim() !== '')
console.log(b) //['Hello there', 'How r you?', 'Thank you']