Remove HTML tags from string

Last update in 10/2019

If you want to remove HTML tags from string for one reason or another, the easiest way will be using regular expressions (regex). Regex in code example below will replace everything inside < and >. This can prevent XSS (cross-site scripting) vulnerability but it is not really flexible (normal content inside < and > will be replaced), so recommended way for doing this in production is using library like DOMPurify.

const cleanHtmlTags = string => {
return string.replace(/<[^>]*>/g, '')
}

And expected result when calling function...

cleanHtmlTags(`<p>Normal content<img onload="alert(hey)"></p>`)
// output: Normal content