Javascript Capitalizes Each Word in the English Title

Updated at: 2020-02-29 Views: 647 Words: 110 Category: Javascript

Most of the content of my English blog comes from the translation of my Chinese blog. In order to save time, I want to format the translated English blog title automatically, that is, the initial of each word in the English title is converted to uppercase.

JavaScript implementation code:

function format_en_title(title) {
    var words = title.trim().toLowerCase().split(' ');
    var ignore_words = ["of", "to", "the", "in"];

    for (var i = 0; i < words.length; i++) {
        if (ignore_words.indexOf(words[i]) > -1 ) {
            continue
        }
        words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1);     
    }

    return words.join(' '); 
}

console.log(format_en_title("I'm a lazy boy in the sky"));

Result:

I'm A Lazy Boy in the Sky

Leave a Comment