Skip to content

Instantly share code, notes, and snippets.

@designfrontier
Created February 2, 2012 16:03
Show Gist options
  • Save designfrontier/1724234 to your computer and use it in GitHub Desktop.
Save designfrontier/1724234 to your computer and use it in GitHub Desktop.
Some code extending the JavaScript string prototype for working with titles and headers
String.prototype.titleCaps = function(){
var thisString = this.replace(/( )([a-z])/g, function(m, $1, $2){return $1 + $2.toUpperCase();});
//remove and, the, or of from caps if they are not the first element in the string
thisString = thisString.replace(/(and|of|to|the|or)/ig, function(m,$1){return $1.toLowerCase();});
thisString = thisString.replace(/(^[a-z])/,function(m,$1){return $1.toUpperCase();});
return thisString;
};
String.prototype.andToAmpEntity = function(){
return this.replace(/(\band\b)/g,'&');
};
String.prototype.andToAmp = function(){
return this.replace(/(\band\b)/g,'&');
};
String.prototype.firstAndToAmpEntity = function(){
return this.replace(/(\band\b)/,'&');
};
String.prototype.firstAndToAmp = function(){
return this.replace(/(\band\b)/,'&');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment