Created
February 2, 2012 16:03
-
-
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
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
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