Last active
December 25, 2015 04:39
-
-
Save Kcko/6918472 to your computer and use it in GitHub Desktop.
JavaScript: string fns prototypes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// stripslashes | |
String.prototype.stripslashes = function(){ | |
return this.replace(/<.*?>/g, ''); | |
}; | |
String.prototype.htmlspecialchars = function(){ | |
var str = this.replace(/&/g, '&'); | |
str = str.replace(/</g, '<'); | |
str = str.replace(/>/g, '>'); | |
str = str.replace(/"/g, '"'); | |
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