jQuery Title Case

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

    You can also implement a pure javascript extension method like this:

    String.prototype.capitalize = function () {
    
        return this.toLowerCase().replace(/\b[a-z]/g, function (letter) {
            return letter.toUpperCase();
        });
    };
    

    And call it like this:

    "HELLO world".capitalize()
    

提交回复
热议问题