Skip to content

Instantly share code, notes, and snippets.

@Kcko
Last active December 25, 2015 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kcko/6918472 to your computer and use it in GitHub Desktop.
Save Kcko/6918472 to your computer and use it in GitHub Desktop.
JavaScript: string fns prototypes
// stripslashes
String.prototype.stripslashes = function(){
return this.replace(/<.*?>/g, '');
};
String.prototype.htmlspecialchars = function(){
var str = this.replace(/&/g, '&amp;');
str = str.replace(/</g, '&lt;');
str = str.replace(/>/g, '&gt;');
str = str.replace(/"/g, '&quot;');
return str;
};
// htmlspecialchars
var str = '<b>my personal website:</b> ';
str += '<a href="http://www.jonasjohn.de/">jonasjohn.de</a>';
document.write("Original string (html): '" + str + "'<br/><br/>");
var str_no_html = str.stripslashes();
document.write("- String without HTML tags: '" + str_no_html + "'<br/>");
var str_hsc = str.htmlspecialchars();
document.write("- String with converted HTML tags: '" + str_hsc + "'");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment