How to Remove HTML Tags from String using JavaScript

Sometimes we get data from the server which contains HTML elements such as div span or internal CSS. We can either remove or parse those tags using backend language or we can achieve the same with javascript replace() function. Using Regex in javascript we can remove any HLMT tag from the string. following is the code to remove the HMLT tags from string.

var str = "<div>Hello, <h2>atcodex</h2></div>";
var textData = str.replace(/<\/?[^>]+(>|$)/g, "");

Spread the love

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *