Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading