Skip to content

Instantly share code, notes, and snippets.

@sayfulloev
Forked from mattwiebe/unCamelCase.js
Last active December 26, 2017 15:25
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 sayfulloev/20a56146c5fed26acb5a24d46ce745fb to your computer and use it in GitHub Desktop.
Save sayfulloev/20a56146c5fed26acb5a24d46ce745fb to your computer and use it in GitHub Desktop.
unCamelCase.js
/**
* Turns someCrazyName into Some Crazy Name
* Decent job of acroynyms:
* ABCAcryonym => ABC Acryoynm
* xmlHTTPRequest => Xml HTTP Request
* It also handles numeric values:
* seller1ID => Seller 1 ID
*/
String.prototype.unCamelCase = function(){
return this
// insert a space between lower/number & upper/number
.replace(/([a-z]|[0-9])([A-Z]|[0-9])/g, '$1 $2')
// space before last upper/number in a sequence followed by lower
.replace(/\b([A-Z]+|[0-9]+)([A-Z])([a-z])/, '$1 $2$3')
// uppercase the first character
.replace(/^./, function (str) {
return str.toUpperCase();
})
}
// Or, one liner.
String.prototype.unCamelCase=function(){return this.replace(/([a-z]|[0-9])([A-Z]|[0-9])/g,'$1 $2').replace(/\b([A-Z]+|[0-9]+)([A-Z])([a-z])/,'$1 $2$3').replace(/^./,function(s){return s.toUpperCase();})}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment