7 Replies Latest reply on Nov 21, 2017 1:26 AM by [Jongware]

    Capitalize first letter of each word with regexp

    subieguy2 Level 1

      I am trying to capitalize the first letter of each word from a user input. The user types an input into a gui for example:

      the name and the title

      I want my regexp to make it look like this....

      The Name and The Title

       

      So capitalize first letter of any word except and.

       

      I thought what I had written was correct but it doesn't seem to be working. I wrote 2 different options and neither work.

      Any help would be appreciated!

       

      var formattedOutput = formatDesc(userInput);                    
      formattedOutput.replace(/\b[a-z]/g, function(f){return f.toUpperCase();})
      

       

      That is not working or addressing the word and either.

       

      I also tried it as a function like this

      function titleCase(str) {
       var splitStr = str.toLowerCase().split(' ');
       for (var i = 0; i < splitStr.length; i++) {
         if (splitStr.length[i] < splitStr.length) {
           splitStr[i].charAt(0).toUpperCase();     
         }
            str = splitStr.join(' '); 
       }
      return str;
      }
      var formattedOutput = formatDesc(userInput);
      titleCase(formattedOutput);
      

       

      Still no dice. What am I doing wrong here?