Title Case a Sentence (need a bit of help)

Tell us what’s happening:
Hello, I used regex to try and complete this challenge but I’m having an issue since \b is not only counting spaces but also single quotes so instead of what I want to happen which is match I in I’m, It’s matching I and M. Is there anything I can do with my regex or is my approach incorrect?

Your code so far



function titleCase(str) {
  let lower = str.toLowerCase();
  console.log(lower);
  let regex = /\b\w/g;
  let str2 = lower.replace(regex, function (x){
    return x.toUpperCase();
  });
  return str2;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence/

\w includes ‘word characters’ which is a wider range than you’re looking for. I believe it includes 0-9 and several common punctuation marks.

Instead try

let regex = /\b[a-z]/gi;

using only the lowercase range prevents the inclusion of unintended characters (I think that’s because regex goes by the numerical value of the characters, and there are some punctuations stored in the range between the lowercase and uppercase alphabets, which are otherwise consecutive). Setting the i flag makes it catch the uppercase characters anyway.