javascript ucwords 1

javascript ucwords

function ucwords (str) {
    return (str + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) {
        return $1.toUpperCase();

Here is what the above code is Doing:
1. We’re using the replace() method to replace the first letter of each word with its uppercase version.
2. The regular expression checks for the first letter of each word (^([a-z])), or for a group of spaces followed by a letter ([\s+([a-z])).
3. The function passed to replace() will be called for each match, with the matched text as the first parameter.
4. The function returns the uppercase version of the matched text.

Similar Posts