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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: