Title case a sentence?

后端 未结 13 1456
醉梦人生
醉梦人生 2021-02-10 01:27

I\'m trying to proper case a string in javascript - so far I have this code: This doesn\'t seem to capitalize the first letter, and I\'m also stuck on how to lowercase all the l

13条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-10 01:45

    I recently redid this problem using regex which matches the first letter and accounts for apostrophe. Hope it's helpful:

    function titleCase(str) {
    
    return str.toLowerCase().replace(/^\w|\s\w/g, function(firstLetter) {
        return firstLetter.toUpperCase();
      });
    }
    titleCase("I'm a little tea pot");
    

提交回复
热议问题