Skip to content

Instantly share code, notes, and snippets.

@chicagoworks
Created December 26, 2010 17:08
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 chicagoworks/755511 to your computer and use it in GitHub Desktop.
Save chicagoworks/755511 to your computer and use it in GitHub Desktop.
String object additions
//==== 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