Blog

Capitalizing Words in Javascript

This is a very old draft stuck at the bottom of my draft list. lol

var str = "hello world";
str = str.toLowerCase().replace(/b[a-z]/g, function(letter) {
    return letter.toUpperCase();
});
alert(str); //Displays "Hello World"

Making it as a function, we can do this.

function capitalize(str) {
    return str.toLowerCase().replace(/b[a-z]/g, function(letter) {
        return letter.toUpperCase();
    });
}

Leave a Reply

%d bloggers like this: