Javascript String htmlEscape()

Description

Javascript String htmlEscape()


String.prototype.htmlEscape = function() {
    return this.replace(/&/g, '&')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;')
        .replace(/'/g, '&#39;');
}
console.log('<script>'.htmlEscape()); // &lt;script&gt;

Javascript String htmlEscape()

String.prototype.htmlEscape = function() {
     return $('<div/>').text(this.toString()).html();
};

Javascript String htmlEscape()

console.log('\nProblem 5. nbsp');
/*Write a function that replaces non breaking white-spaces in a text with &nbsp*/


String.prototype.htmlEscape = function (){
    var escapedStr = String(this).replace(/&/g, '&amp;');
    escapedStr = escapedStr.replace(/\s/g, '&nbsp;');
    return escapedStr;
}

var text = 'This is for testing';
console.log(text);//ww  w  .ja  va 2  s.c  o m
text = text.htmlEscape(); //because string is immutable type
console.log(text);

Javascript String htmlescape()

String.prototype.htmlescape = function () {
 return//from  ww  w.  java2 s. co  m
  this.replace(/&/g,'&amp;').
       replace(/>/g,'&gt;').
       replace(/</g,'&lt;').
       replace(/"/g,'&quot;');
};

Javascript String htmlEscape()

String.prototype.htmlEscape = function(){
    var span = document.createElement('span');
    var txt =  document.createTextNode('');
    span.appendChild(txt);/*w w  w  . j  a  v a2 s .  c  om*/
    txt.data = this;
    return span.innerHTML;
};

Javascript String htmlEscape()

String.prototype.htmlEscape = function (){
  var escapedStr = String(this).replace(/&/g, '&amp;');
  escapedStr = escapedStr.replace(/</g, '&lt;');
  escapedStr = escapedStr.replace(/>/g, '&gt;');
  escapedStr = escapedStr.replace(/"/g, '&quot;');
  escapedStr = escapedStr.replace(/'/g, "&#39");
  return escapedStr;
}

Javascript String htmlescape()

String.prototype.htmlescape = function () {
 return(/* w w w  .j a va 2 s  . c om*/
  this.replace(/&/g,'&amp;').
       replace(/>/g,'&gt;').
       replace(/</g,'&lt;').
       replace(/"/g,'&quot;')
 );
};



PreviousNext

Related