function ucwords (str) { return (str + '').replace(/^([a-z])|s+([a-z])/g, function ($1) { return $1.toUpperCase(); }); } console.log(ucwords("hello world"));