Javascript如何将纯文本html元素拆分为数组? [英] Javascript how to split plain text html elements into an array?

查看:40
本文介绍了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']

这篇关于Javascript如何将纯文本html元素拆分为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆