Created
December 26, 2010 17:08
-
-
Save chicagoworks/755511 to your computer and use it in GitHub Desktop.
String object additions
This file contains hidden or 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
//==== General Enhancements to Javascript =========================================================== | |
// Replace repeated spaces, newlines and tabs with a single space | |
String.prototype.normalize = function() {return this.replace(/^\s*|\s(?=\s)|\s*$/g, "");} | |
//http://www.somacon.com/p355.php | |
//Trim: Strip leading and trailing white-space | |
//NOTE: can also use jQuery.trim | |
String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g,"");} | |
String.prototype.ltrim = function() {return this.replace(/^\s+/,"");} | |
String.prototype.rtrim = function() {return this.replace(/\s+$/,"");} | |
//implement a swapcase mehtod on String prototype | |
//http://snippets.dzone.com/posts/show/770 | |
String.prototype.swapcase = function(){ | |
return this.replace(/([a-z]+)|([A-Z]+)/g,function($0,$1,$2){ | |
return ($1) ? $0.toUpperCase() : $0.toLowerCase(); | |
}) | |
} | |
//stripHTML on String prototype | |
//http://snippets.dzone.com/posts/show/6378 | |
String.prototype.stripHTML = function() {return this.replace(/<(?:.|\s)*?>/g, "");}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment