Skip to content
Advertisement

Remove certain character from string and return into an array

I’ve got a string that has html tag like this:

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

so i can get return result in array like this :

['example 1st','example 2nd']

Advertisement

Answer

You can create a div and then find all the text using innerText using querySelectorAll and array#map.

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);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement