Capitalize the first letter of every word

前端 未结 8 1778
梦谈多话
梦谈多话 2020-12-09 17:21

I want to use a javascript function to capitalize the first letter of every word

eg:

THIS IS A TEST ---> This Is A Test
this is a TEST ---> Th         


        
8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-09 18:14

    If you don't mind using a library, you could use Sugar.js capitalize()

    capitalize( all = false ) Capitalizes the first character in the string and downcases all other letters. If all is true, all words in the string will be capitalized.

    Example:

    'hello kitty'.capitalize()     -> 'Hello kitty'
    'hello kitty'.capitalize(true) -> 'Hello Kitty'
    

提交回复
热议问题