Javascript String stripHtml()

Description

Javascript String stripHtml()


?
// Extension method stripHtml for string object
String.prototype.stripHtml = function () {
    return this.replace(new RegExp(/<[^>]+>/g), "");
};

Javascript String stripHTML()

String.prototype.stripHTML = function(){
  return this.replace(/<(?:.|\s)*?>/g, "");
};

Javascript String stripHtml()

String.prototype.stripHtml = function () {
    var regex = /(<([^>]+)>)/ig
    return this.replace(regex, "");
}

Javascript String stripHtml()

String.prototype.stripHtml = function () {

    var self = this;

    var text = self.replace(/&(lt|gt);/g, function (strMatch, p1) {
        return (p1 == "lt") ? "<" : ">";
    });//from   ww  w.j a v a2  s  . c  o  m
    return text.replace(/<\/?[^>]+(>|$)/g, "");
}

Javascript String stripHtml()

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
String.prototype.stripHtml = function() {
  return this.replace(/<[^>]+>/g, "");
};

console.assert("<p>Shoplifters of the World <em>Unite</em>!</p>".stripHtml() == "Shoplifters of the World Unite!");
console.assert("1 &lt; 2".stripHtml() == "1 &lt; 2");

Javascript String stripHtml()

String.prototype.stripHtml = function() {
   var tmp = document.createElement("DIV");
   tmp.innerHTML = this;//w ww.j  av a  2 s . c o m
   return tmp.textContent || tmp.innerText || "";
};

Javascript String stripHtml()

String.prototype.stripHtml = function () {
    //got regex from http://www.dotnetperls.com/remove-html-tags
    var pattern = /<.*?>/g;
    return this.replace(pattern, "");
}

Javascript String stripHtml()

// Requirement 6//from   www  . j a v a 2  s  . c  o m
String.prototype.stripHtml = function () {
  // Sandbox http://www.regexpal.com/?fam=96086
  // Modifiers used g=global m=multiline
  return this.replace(/<(?:.|\n)*?>/gm, '');
};

Javascript String stripHtml()

//Assumption: this is to be written with a regular expression and 
//not processed by the browser, like this example. 
//var div = document.createElement("DIV");
//div.innerHTML = stringWithHtml;
//return div.textContent;

String.prototype.stripHtml = function () {
 //  [^>]  ([^x]) A negated or complemented character set. That is, it matches anything that is not enclosed in the brackets.
 //  +  Matches the preceding expression 1 or more times. 
 //  /i   Perform case-insensitive matching.
 // /g   Perform a global match, finds all matches.
 "use strict";/*from w ww  . java  2 s  .c  om*/

 var htmlRegex = /(<([^>]+)>)/ig;
 return this.replace(htmlRegex, '');
};

Javascript String stripHTML()

/**/* w w w.j  av  a2 s .  c o  m*/
@Name: String.prototype.stripHTML
@Author: Paul Visco
@Version: 1.0 11/19/07
@Description: Removes all HTML tags from a string
@Return: String The original string without any HTML markup
@Example:
var myString = 'hello <p>world</p> on earth';

var newString = myString.stripHTML();
//newString = 'hello world on earth'
*/
String.prototype.stripHTML = function(){
 var re = new RegExp("(<([^>]+)>)", "ig");
 var str = this.replace(re, "");
 var amps = ["&nbsp;", "&amp;", "&quot;"];
 var replaceAmps =[" ", "&", '"'];
 for(var x=0;x<amps.length;x++){
  str = str.replace(amps[x], replaceAmps[x]);
 }
 
 re = new RegExp("(&(.*?);)", "ig");
 str = str.replace(re, "");
 
 return str;
};



PreviousNext

Related