Javascript String encodeHTML()
method
function encodeHTMLSource() { var encodeHTMLRules = { "&": "&", "<": "<", ">": ">", '"': '"', "'": ''', "/": '/'}, matchHTML = /&(?!#?\w+;)|<|>|"|'|\//g; return function() { return this ? this.replace(matchHTML, function(m) {return encodeHTMLRules[m] || m; }) : this; };//from w w w.j a v a2 s . c o m } String.prototype.encodeHTML = encodeHTMLSource();
/**//from w w w. j a v a 2 s .co m * Add HTML encode function to String. * * @returns {String} the HTML encoded string */ String.prototype.encodeHTML = function () { return this.replace(/&/g, '&') .replace(/</g, '<') .replace(/>/g, '>') .replace(/"/g, '"'); };
String.prototype.encodeHtml = function () { var str = this; var s = ""; if (str.length == 0) return ""; s = str.replace(/&/g, ">"); s = s.replace(/</g, "<"); s = s.replace(/>/g, ">"); s = s.replace(/ /g, " "); s = s.replace(/\'/g, "'"); s = s.replace(/\"/g, """); s = s.replace(/\n/g, "<br>"); return s; //from ww w . j a v a2 s . c o m }