Uppercase first letter of variable

后端 未结 23 1467
粉色の甜心
粉色の甜心 2020-11-30 20:55

I have searched over the web can can\'t find anything to help me. I want to make the first letter of each word upper case within a variable.

So far i have tried:

23条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 21:04

    Here is unicode-safe ucwords() function, which additionally respects double-lastnames like Russian Засс-Ранцев and some noble names like Honoré de Balzac, d'Artagnan, Vincent van Gogh, Otto von Bismarck, Sulaymān ibn Dāwūd, etc:

    String.prototype.ucwords = function() {
      return this.toLowerCase()
        .replace(/(^|\s|\-)[^\s$]/g, function(m) {
           return m.toUpperCase();
        })
        // French, Arabic and some noble names...
        .replace(/\s(Of|De|Van|Von|Ibn|Из|Ван|Фон|Ибн)\s/g, function(m) { // Honoré de Balzac, Vincent van Gogh, Otto von Bismarck, Sulaymān ibn Dāwūd etc.
           return m.toLowerCase();
        })
        .replace(/(^|\s)(D|Д)(['’][^\s$])/g, function(m, p1, p2, p3) { // D'Artagnan or d'Artagnan / Д’Артаньян или д’Артаньян
           return p1 + (p1 === "" ? p2/*.toUpperCase()*/ : p2.toLowerCase()) + p3.toUpperCase();
        });
    }
    

提交回复
热议问题