leodutra
3/21/2013 - 6:00 PM

Encodes (escapes) HTML Special Chars

Encodes (escapes) HTML Special Chars

function htmlSpecialCharsEntityEncode(str) {
  return str.replace(/[<>&\r\n"']/gm, function (match) {
    return '&' + {
      '<': 'lt;',
      '>': 'gt;',
      '&': 'amp;',
      '\r': "#13;",
      '\n': "#10;",
      '"': 'quot;',
      "'": 'apos;' /*single quotes just to be safe*/
    }[match];
  });
}