I’ve got a string that has html tag like this:
JavaScript
x
2
1
var list = "<ul><li><p>example 1st</p></li><li><p>example 2nd</p></li></ul>"
2
how can I remove every character of The u, p, li tags
so i can get return result in array like this :
JavaScript
1
2
1
['example 1st','example 2nd']
2
Advertisement
Answer
You can create a div
and then find all the text using innerText
using querySelectorAll
and array#map
.
JavaScript
1
5
1
const list = "<ul><li><p>example 1st</p></li><li><p>example 2nd</p></li></ul>";
2
const div = document.createElement('div');
3
div.innerHTML = list;
4
const text = [div.querySelectorAll('ul li p')].map(element => element.innerText);
5
console.log(text);