A Live Developer Journal

Title Case A Sentence With Regex

Problem Statement

Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.

For the purpose of this exercise, you should also capitalize connecting words like "the" and "of".


In my original solution (shown below), I first split my str into lowercase words. Then I mapped my word array into a new array where the first letter of each word was capitalized, before joining the new title-cased words into a new setence:

function titleCase(str) {
  const lowerCaseWords = str.toLowerCase().split(" ");

  lowerCaseWords.map(word => word.replace(word[0], word[0].toUpperCase()));

  return lowerCaseWords.join(" ");

titleCase("I'm a little tea pot");

After doing this, I thought it'd be cool to try and solve the same problem, but this time using a regex approach. Here is what I came up with:

function titleCase(str) {
  const lowerCase = str.toLowerCase();

  return lowerCase.replace(/\w\S*/g, x => x.charAt(0).toUpperCase() + x.substr(1).toLowerCase());

titleCase("I'm a little teapot");

I prefer my first approach because it's easier to read, but this was fun to figure out.