Title case a sentence help!

Im having trouble with this algorithm challenge.
my code works for all cases except for this instance I get “I’M A Little Tea Pot” I cant figure out how to keep the M and the A from capitalizing. I cant figure the proper REGEX to prevent this. Any help would be appreciated!

function titleCase(str) {
  var string = str.toLowerCase();
  function upper(match){
    return match.toUpperCase();
  return string.replace(/\b(\w)/g, upper);

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

When I get stuck with regex I turn to http://regex101.com to work stuff out. If that fails you could fall back on if statements that might help…but that won’t be as clean a solution :slight_smile:

Also, I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

1 Like