function encodeHTMLSource() { var encodeHTMLRules = { "&": "&", "<": "<", ">": ">", '"': '"', "'": ''', "/": '/', "\n": '
' }, matchHTML = /&(?!#?\w+;)|<|>|"|'|\/\n/g; return function() { return this ? this.replace(matchHTML, function(m) {return encodeHTMLRules[m] || m;}) : this; }; } String.prototype.encodeHTML = encodeHTMLSource();