capitalize first letter
of each word in a string
[ regular expression | replace | toLowerCase | toUpperCase ]

Capitalize first letter of each word in a string

Write a function that takes a string and returns a new string with the first letter of each word capitalized.

The rest of the letters in the word are converted to lower case.


Example ...

Enter a string ...

"once in a BLUE moon" original string

"Once In A Blue Moon" returns new string with capitalized words

The first letter of each word is capitalized.

The rest of the letters in the word are converted to lower case.

The original string is unchanged.

Strings are a sequence of zero or more characters written inside quotes used to represent text.

Strings may consist of letters, numbers, symbols, words, or sentences.

Strings are immutable, they cannot be changed.

Each character in a string has an index.

The first character will be index 0 the second character will be index 1 and so on.

There are two ways to access an individual character in a string.

charAt() method

const str1 = "abc"; string

str1.charAt(0); character at index 0 → "a"

str1.charAt(1); character at index 1 → "b"

str1.charAt(2); character at index 2 → "c"

str1.charAt(3); character at index 3 → "" index not found

Alternatively use at() or slice() methods

bracket notation []

const str2 = "abc"; string

str2[0]; character at index 0 → "a"

str2[1]; character at index 1 → "b"

str2[2]; character at index 2 → "c"

str2[3]; character at index 3 → undefined index not found


Capitalize first character of each word using ↴

Regular Expression → patterns used to match character combinations in strings.

replace() method → returns a new string with the value(s) replaced.

toLowerCase() method → returns the value of the string converted to lower case.

toUpperCase() method → returns the value of the string converted to upper case.


Regular expressions are patterns used to match character combinations in strings.

The Regular Expression /(^|\s)[a-z]/gi; matches the first letter of each word.

(^|\s) checks for the start of the string or a whitespace character.

^ caret symbol denotes the start of the string.

\s denotes any whitespace character.

[a-z] checks for any lower case letter from "a" to "z"

g flag indicates that the search should be global, meaning it will find all occurrences.

i flag makes the search case-insensitive, allowing it to match both upper case and lower case characters.


replace() method searches a string for a value or a regular expression and returns a new string with the value replaced.

If pattern is a string, only the first occurrence will be replaced. The original string is unchanged.

syntax

string.replace(pattern, replacement)

const str3 = "hello world";

str3.replace("hello", "HELLO"); replace "hello" with "HELLO"

returns ↴

"HELLO world"

const str4 = "ABCDEF";

str4.replace("B", ""); replace "B" with "" empty string

returns ↴

"ACDEF""B" removed from string


toLowerCase() method returns a new string with all letters converted to lower case. The original string is unchanged.

const str5 = "hELlo wORLd";

str5.toLowerCase(); returns ↴

"hello world" → lower case


toUpperCase() method returns a new string with all letters converted to upper case. The original string is unchanged.

const str6 = "hELlo wORLd";

str6.toUpperCase(); returns ↴

"HELLO WORLD" → upper case


Initialize a variable to hold the string to capitalize first letter of each word.

const string1 = "hELlo wORLd"; → user input


Define a function capWords to capitalize first letter of each word.

function capWords(str) {}

The function takes a string as input str and returns a new string with the first letter of each word capitalized. The original string remains unchanged.

Convert str to lower case.

const lowerStr = str.toLowerCase() lowerStr

The regular expression searches for the first letter in the word ↴

/(^|\s)[a-z]/gi

and replaces the lower case letter with an upper case letter.

(char) => char.toUpperCase()

char → current element being processed in the string

Capitalize first letter of each word and return.

return lowerStr.replace(/(^|\s)[a-z]/gi, (char) => char.toUpperCase())


Call the function with ↴

capWords(string1);


Capitalize first letter of each word.

const string1 = "hELlo wORLd";

function capWords(str) {

const lowerStr = str.toLowerCase();

return lowerStr.replace(/(^|\s)[a-z]/gi, (char) => char.toUpperCase());

}

call function

capWords(string1); returns ↴

"Hello World"


Alternative using different regular expression pattern ↴

return lowerStr.replace(/(^|\s)[a-z]/gi, (char) => char.toUpperCase());

return lowerStr.replace(/\b\w/g, (char) => char.toUpperCase());

Capitalize the first letter of each word in a string