Split up a camel-cased word at the humps, insert separator and lowercase the character after it.
-
-
Save tkissing/1347102 to your computer and use it in GitHub Desktop.
getOffTheCamel - 140byt.es splitCamelCaseString in JavaScript
This file contains 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
function(a,b) { // a: string in camelCase format [b: separator; defaults to SPACE] | |
// coerce argument to string | |
return (a+'').replace( | |
// search for uppercase (ascii) characters following anything that is not an uppercase (ascii) character | |
/([^A-Z])([A-Z]+)?([A-Z])/g, | |
function(c,d,e,f) { | |
// leave the not-uppercase-character as is, append separator, append the hump (in lowercase unless its multiple uppercase chars in a row) | |
return [d,b||' ',e?e+f:f.toLowerCase()].join(''); | |
} | |
); | |
}; |
This file contains 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
function(a,b){return(a+'').replace(/([^A-Z])([A-Z]+)?([A-Z])/g,function(c,d,e,f){return[d,b||' ',e?e+f:f.toLowerCase()].join('')})} |
This file contains 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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Timo Kissing http://kissing.name | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains 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
{ | |
"name": "getOffTheCamel", | |
"description": "JavaScript function to split a camelCase word at the humps", | |
"keywords": [ | |
"140bytes", | |
"string", | |
"camelCase" | |
] | |
} |
This file contains 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
<!DOCTYPE html> | |
<head> | |
<title>getOffTheCamel -> get off the camel</title> | |
</head> | |
<body> | |
<div>Expected value: <b>"Click fork to play!","byte-saving", "get USBController adapter"</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
// write a small example that shows off the API for your example | |
// and tests it in one fell swoop. | |
var getOffTheCamel = function(a,b){return(a+'').replace(/([^A-Z])([A-Z]+)?([A-Z])/g,function(c,d,e,f){return[d,b||' ',e?e+f:f.toLowerCase()].join('')})}; | |
document.getElementById( "ret" ).innerHTML = [getOffTheCamel("ClickForkToPlay!"), getOffTheCamel("byteSaving", '-'), getOffTheCamel("getUSBControllerAdapter")]; | |
</script> | |
</body> | |
</html> |
tsaniel: Very nice regex-magic there. I like the lowercasing, so will stick with my version. But using a lookahead might help handling multiple uppercase characters in a row. Currently passing in "getUSBController" gives "get uSBController" using my version and "get U S B Controller" using your version - would love to have it return "get USB Controller" or at least "get USBController" (iow, don't lowercase if the hump consists of multiple uppercase characters
Ok, no need for lookaheads. "getUSBController" now turns into "get USBController". If you'd rather have "get USB controller", try this:
function(a,b){b=b||' ';return(a+'').replace(/([^A-Z])([A-Z]+)?([A-Z])/g,function(c,d,e,f){return[d,b,e,e?b:e,f.toLowerCase()].join('')})}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What about
?