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, Sulayman ibn Dawud, 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, Sulayman ibn Dawud 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();
});
}