Skip to content

Instantly share code, notes, and snippets.

@jacobmc
Created March 29, 2021 15:30
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 jacobmc/ffbe6541fc1386e7626a72110df5b450 to your computer and use it in GitHub Desktop.
Save jacobmc/ffbe6541fc1386e7626a72110df5b450 to your computer and use it in GitHub Desktop.
Convert string to camel case in Javascript
/**
* 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