How to uncamelize a string in Javascript? Example: "HelloWorld" --> "hello_world"
/**
* Uncamelize a string, joining the words by separator character.
* @param string Text to uncamelize
* @param string Word separator
* @return string Uncamelized text
*/
function uncamelize(text, separator) {
// Assume separator is _ if no one has been provided.
if(typeof(separator) == "undefined") {
separator = "_";
}
// Replace all capital letters by separator followed by lowercase one
var text = text.replace(/[A-Z]/g, function (letter) {
return separator + letter.toLowerCase();
});
// Remove first separator (to avoid _hello_world name)
return text.replace("/^" + separator + "/", '');
}