Convert special chars to HTML entities - Node.js String

Node.js examples for String:Char

Description

Convert special chars to HTML entities

Demo Code

/**/* w  ww. ja  v a 2s.  c  o  m*/
 * Convert special chars to HTML entities
 * @addon
 * @return The string with chars encoded for HTML
 * @type String
 */
String.prototype.htmlEnc = function() {
  var str = this.replace(/&/g,"&");
  str = str.replace(/</g,"&lt;");
  str = str.replace(/>/g,"&gt;");
  str = str.replace(/\"/g,"&quot;");
  str = str.replace(/\n/g,"<br />");
  return str;
};

Related Tutorials