Challenge 25

👨🏻‍💻

HTML Text Parser

Open in Code Editor or Copy Instructions
...solutions at bottom of page...

Create a function that is able to extract the text from an HTML string

HTML elements are comprised of opening tags and closing tags - the format for both are as follows: opening <tagname> closing </tagname>. Note how the closing tags have the forward slash / before the tag name

Examples:

extractText("<p>HTML stands for Hypertext Markup Language</p>" returns HTML stands for Hypertext Markup Language

extractText("<h1>Visit the</h1><a href="https://info.cern.ch/"> first ever website</p>" returns Visit the first ever website

extractText("<h3>It was created by</h3><p fake="<p>won't show</p>"> Sir Tim Berners-Lee</p>" returns It was created by Sir Tim Berners-Lee

Extension:

Ensure your code works for nested elements: extractText("<div>1<p>2<span>3</span>2</p>1</div>" returns 12321

Try to add some kind of validation - e.g. extractText("<div>1<p>2<span>3</span>2<p>1<div>" could return 2 tags were never closed using the "/"

Solutions

A-Level 9618