String prototypes

Shows how to create a String prototypes

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;
};
Snippet Details



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 + "'");

Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

None.