Strip HTML tags from string using JavaScript

Stripping HTML from string is easy to do in JavaScript. Either we are doing this on a browser or in the server-side we can easily do this. Following are some techniques to achieve this:

Using JavaScript

var tmp = document.createElement("div");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || "";

Using jQuery

jQuery(html).text(); 
jQuery("<p>").html(html).text();

Using regular expression

html.replace(/<(?:.|\n)*?>/gm,'');

Comments