Javascript - Strip HTML Tags

Javascript - Strip HTML Tags

HTML

<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<p class="well info"></p>


<div class="hiddentext" style="display:none;">
<div class="bc-content-wrapper">\n<p>\u0e01\u0e32\u0e23\u0e43\u0e0a\u0e49 since, for</p><ul><li>\u0e21\u0e31\u0e01\u0e43\u0e0a\u0e49\u0e43\u0e19 present perfect tense</li><li>since \u0e15\u0e32\u0e21\u0e14\u0e49\u0e27\u0e22 \u0e08\u0e38\u0e14\u0e40\u0e23\u0e34\u0e48\u0e21\u0e15\u0e49\u0e19\u0e02\u0e2d\u0e07\u0e40\u0e27\u0e25\u0e32 (point of time)</li><li>for \u0e15\u0e32\u0e21\u0e14\u0e49\u0e27\u0e22 \u0e23\u0e30\u0e22\u0e30\u0e40\u0e27\u0e25\u0e32 (duration)</li></ul>\n</div>\n<style>\n@import url(\"https://fonts.googleapis.com/css?family=Prompt:300,500,700\");\r\n.bc-content-wrapper {\r\n  color: #4a4a4a;\r\n  font-size: 14px;\r\n  line-height: 1.6em;\r\n}\r\n\r\n.bc-content-wrapper img{\r\n    max-width: 100%;\r\n}\r\n\r\n.bc-content-wrapper h1,\r\n.bc-content-wrapper h2,\r\n.bc-content-wrapper h3,\r\n.bc-content-wrapper h4,\r\n.bc-content-wrapper h5,\r\n.bc-content-wrapper h6 {\r\n  font-family: \"Prompt\", sans-serif;\r\n  font-size: 20px;\r\n  font-weight: 500;\r\n  margin: 30px 0 15px;\r\n  letter-spacing: 0.02em;\r\n  line-height: 1.4em;\r\n}\r\n.bc-content-wrapper h1:first-child,\r\n.bc-content-wrapper h2:first-child,\r\n.bc-content-wrapper h3:first-child,\r\n.bc-content-wrapper h4:first-child,\r\n.bc-content-wrapper h5:first-child,\r\n.bc-content-wrapper h6:first-child {\r\n  margin-top: 0;\r\n}\r\n.bc-content-wrapper h1:last-child,\r\n.bc-content-wrapper h2:last-child,\r\n.bc-content-wrapper h3:last-child,\r\n.bc-content-wrapper h4:last-child,\r\n.bc-content-wrapper h5:last-child,\r\n.bc-content-wrapper h6:last-child {\r\n  margin-bottom: 0;\r\n}\r\n.bc-content-wrapper h1 {\r\n  font-size: 26px;\r\n}\r\n.bc-content-wrapper h2 {\r\n  font-size: 20px;\r\n}\r\n.bc-content-wrapper h3{\r\n  font-size: 16px;\r\n}\r\n.bc-content-wrapper h4,\r\n.bc-content-wrapper...

JavaScript

var n = "";
n = $(".hiddentext").text();
/* $(".info").append("<br>---> "+n);
n = n.replace(/(<([^>]+)>)/ig,"");
$(".info").append("<br>---> "+n); */


var ttt = strip(n);
$(".info").append("<br>---> "+ttt);

function convertHtmlToText(inputText) {
    var returnText = "" + inputText;

    //-- remove BR tags and replace them with line break
    returnText=returnText.replace(/<br>/gi, " ");
    returnText=returnText.replace(/<br\s\/>/gi, " ");
    returnText=returnText.replace(/<br\/>/gi, " ");

    //-- remove P and A tags but preserve what's inside of them
    returnText=returnText.replace(/<p.*>/gi, "");
    returnText=returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, "$2");

    //-- remove all inside SCRIPT and STYLE tags
    returnText=returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
    returnText=returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
    //-- remove all else
    returnText=returnText.replace(/<(?:.|\s)*?>/g, "");

    //-- get rid of more than 2 multiple line breaks:
    returnText=returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, " ");

    //-- get rid of more than 2 spaces:
    returnText = returnText.replace(/ +(?= )/g,'');

    //-- get rid of html-encoded characters:
    returnText=returnText.replace(/&nbsp;/gi," ");
    returnText=returnText.replace(/&amp;/gi,"&");
    returnText=returnText.replace(/&quot;/gi,'"');
    returnText=returnText.replace(/&lt;/gi,'<');
    returnText=returnText.replace(/&gt;/gi,'>');

		return returnText;
}


function strip(html)
{
    html=html.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
   var tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   return tmp.textContent || tmp.innerText || "";
}