Created
March 29, 2021 15:30
-
-
Save jacobmc/ffbe6541fc1386e7626a72110df5b450 to your computer and use it in GitHub Desktop.
Convert string to camel case 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
/** | |
* Converts string to camel case | |
* | |
* Changes characters following a hyphen, underscore, space, or period. | |
* | |
* Thanks @vitaly-t for this function (https://stackoverflow.com/questions/2970525/converting-any-string-into-camel-case#answer-57927739) | |
*/ | |
function camelize(text) { | |
text = text.replace(/[-_\s.]+(.)?/g, (_, c) => c ? c.toUpperCase() : ''); | |
return text.substr(0, 1).toLowerCase() + text.substr(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment