Convert camel case to human readable string?
Split by non-words; capitalize; join:
function toCapitalizedWords(name) {
var words = name.match(/[A-Za-z][a-z]*/g) || [];
return words.map(capitalize).join(" ");
}
function capitalize(word) {
return word.charAt(0).toUpperCase() + word.substring(1);
}
Extract all words with a regular expression. Capitalize them. Then, join them with spaces.
Example regexp:
/^[a-z]+|[A-Z][a-z]*/g
/ ^[a-z]+ // 1 or more lowercase letters at the beginning of the string
| // OR
[A-Z][a-z]* // a capital letter followed by zero or more lowercase letters
/g // global, match all instances
Example function:
var camelCaseToWords = function(str){
return str.match(/^[a-z]+|[A-Z][a-z]*/g).map(function(x){
return x[0].toUpperCase() + x.substr(1).toLowerCase();
}).join(' ');
};
camelCaseToWords('camelCaseString');
// Camel Case String
camelCaseToWords('thisIsATest');
// This Is A Test