3 ways to split and remove HTML from a string

Nicky Christensen
Frontend Weekly
Published in
3 min readJan 27, 2022

--

Consider the possibility that you are going headless or building an SaaS application and are not relying on a server side programming language to do the work for you, you might sometimes find yourself in need of needing to remove HTML from a string. In this article, I’ll show you a few simple ways on how you can split and remove all HTML from a string.

TIP: You can find my preferred way in the bottom of the article

Using Regex

First off, let me just say, I’m personally not a big fan of regular expressions. They are hard to read and really obscure, and it always seems like a big mystery on what is going on. Despite this, it is really powerful, and by using regular expressions we can easily remove html from strings, and there are great tools to help you in understanding them better like: https://regex101.com/ and https://regexr.com/

const someHTMLString = "<div><h1>A heading.</h1><p>Here we have some text</p></div>";const myString = someHTMLString.replace(/<[^>]+>/g, '');
console.log(myString); //Will print A heading. Here we have some text

Using jQuery

If you are still using jQuery in your projects, you can remove HTML from a string fairly simple. It’s…

--

--

Nicky Christensen
Frontend Weekly

FE Lead at Rig.dev - A danish guy who speaks about frontend, tech & leadership - Follow & connect @ https://www.linkedin.com/in/dknickychristensen/