Convert camel case to human readable string?

1.21 gigawatts picture 1.21 gigawatts · Jan 15, 2014 · Viewed 7.5k times · Source

Is there a reg exp or function that will convert camel case, css and underscore to human readable format? It does not need to support non-humans at this time. Sorry aliens. :(

Examples:

helloWorld -> "Hello World"
hello-world -> "Hello World"
hello_world -> "Hello World"

Answer

Ry- picture Ry- · Jan 15, 2014

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);
}