jQuery Title Case

后端 未结 13 2156
小鲜肉
小鲜肉 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:25

    There isn't anything built-in to jQuery that does it, but you can checkout this site that has a basic code example:

    http://jamesroberts.name/blog/2010/02/22/string-functions-for-javascript-trim-to-camel-case-to-dashed-and-to-underscore/

    String.prototype.toCamel = function(){
        return this.replace(/(\-[a-z])/g, function($1){return $1.toUpperCase().replace('-','');});
    };
    

    It would seem that from there you could call the code like so:

    var str = "my string to camel case";
    str = str.toCamel();
    if ( typeof console !== 'undefined' ) console.log(str);
    

提交回复
热议问题