For better understanding, is there a way to finish the title case code challenge?

For better understanding, is there a way to finish the title case code challenge?
0

#1

Tell us what’s happening:
Referencing past challenges, I didn’t want to use arrays or loops. I thought it needed to be something using expressions ie /\d+/g. I also used the resource link at: Using an inline function that modifies the matched characters, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter

As a beginner to JavaScript, is there any way that this logic would work or could be completed? I could get the sentence to be either entire caps or lowercase, not how to break the code apart into words or spaces (with w or s). Using match, I had to remove the boolean values, I had no idea I needed the “^”, but was testing it out to get a solution with the training material so far and what I could look up. Thank you!

edit: for clarification and I’m also trying to understand Replace’s RegExp a lot better

Your code so far


  function titleCase(str) {
  function lowerToUpperCase (match, offset, string) {
    return (offset > 0, "" ) + match.toLowerCase();
  }
  
  //if after a space, capitalize the first letter
  
        return str.replace(/[A-Z]+/g, lowerToUpperCase);
  //need to change it to only the first letter & drop the boolean value
  //required to turn it into an array to grab the first character??

    //return str.charAt(0).toUpperCase(); Capitalize first character
 
}

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

Your browser information:

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

Link to the challenge:


#2

I solved this challenge using replace and a function. They key is to get the correct regular expression. You really only need the match argument. My solution first converted the entire string to lowercase. Next, my regular expression matched the first character of the string and any situation where a word character followed any character that was not a word character, dash, or apostrophe. This would only match a single character and then my replace callback function would return this letter after it first converted it to uppercase (using toUpperCase).