Skip to content

Instantly share code, notes, and snippets.

@yanil3500
Created November 9, 2019 03:57
Show Gist options
  • Save yanil3500/a59c4df6689302d4bbe90a7dae96cf3b to your computer and use it in GitHub Desktop.
Save yanil3500/a59c4df6689302d4bbe90a7dae96cf3b to your computer and use it in GitHub Desktop.
[Decamelize] Decamelizes a string with/without a custom separator (underscore by default) #javascript #camelcase #utility
/**
* Decamelizes a string with/without a custom separator (underscore by default).
*
* @param str String in camelcase
* @param separator Separator for the new decamelized string.
*/
function decamelize(str, separator){
separator = typeof separator === 'undefined' ? '_' : separator;
return str
.replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2')
.replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + separator + '$2')
.toLowerCase();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment