[英]Remove certain character from string and return into an array
I've got a string that has html tag like this:我有一个带有 html 标签的字符串,如下所示:
var list = "<ul><li><p>example 1st</p></li><li><p>example 2nd</p></li></ul>"
how can I remove every character of The u, p, li tags如何删除 u、p、li 标签的每个字符
so i can get return result in array like this:所以我可以像这样在数组中得到返回结果:
['example 1st','example 2nd']
You can create a div
and then find all the text using innerText
using querySelectorAll
and array#map
.您可以创建一个
div
,然后使用querySelectorAll
和array#map
使用innerText
查找所有文本。
const list = "<ul><li><p>example 1st</p></li><li><p>example 2nd</p></li></ul>"; const div = document.createElement('div'); div.innerHTML = list; const text = [...div.querySelectorAll('ul li p')].map(element => element.innerText); console.log(text);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.