Javascript: Convert proper name to Capital/Title Case

PHOTO EMBED

Mon Aug 29 2022 14:38:23 GMT+0000 (Coordinated Universal Time)

Saved by @marcopinero #javascript

function capitalTitle(s) {
    var preposiciones = [
      'a','al','ante','bajo','cabe','con','contra','de','del','desde','durante',
      'en','entre','hacia','hasta','mediante','para','por','según','sin',
      'so','sobre','tras','versus','via','vs','las','los','y','x','o','ó'
    ];
  
    s=capital(s);
    var r='\\s+('+preposiciones.join("|")+')\\s+';
    var reg=new RegExp(r, "ig");
  
    s = s.replace(reg, function(match) {
        return match.toLowerCase();
    });
  
    s=s.replace(/cxc/gi,'CxC');
    s=s.replace(/cxp/gi,'CxP');
    s=s.replace(/srl/gi,'SRL');
    s=s.replace(/llc/gi,'LLC');
    s=s.replace(/ars/gi,'ARS');
    return s;
}

String.prototype.toTitleCase = function () {
    return this.toLowerCase().replace(/[^\s|\'\-]+/g, function(word) {
        return word.replace(/^./, function(first) {
            return first.toUpperCase();
        });
    });
} 
content_copyCOPY

Get proper names in Capital Case format: example: ÓLIVER DE LAS CASAS PIÑERO => Óliver De Las Casas Piñero