jQuery Title Case

后端 未结 13 2157
小鲜肉
小鲜肉 2020-12-12 22:54

Is there a built in way with jQuery to \"title case\" a string? So given something like bob smith, it turns into \"Bob Smith\"?

13条回答
  •  再見小時候
    2020-12-12 23:32

    is way more simple...
    You have to use a callback in replace.

    toCamelCase = function(str){
      return str.replace(/-\w/g,function(match){return match[1].toUpperCase()})
    }
    // this works for css properties with "-" 
    // -webkit-user-select => WebkitUserSelect
    

    You can change the RegExp to /[-\s]\w/g or /(^|[-\s])\w/g or other...

提交回复
热议问题