0

How to capitalize all words in a string excluding some hardcoded cases? For example: words to exclude: "de", "la" input: "DEGAS-URB.JARDÍN DE LAS AVENIDAS" output: "Degas-Urb.Jardín de las Avenidas"

Gabrielius B.
  • 71
  • 2
  • 4
  • 12

1 Answers1

1

You can use this function on each words:

function toTitleCase(str)
{
    // Protected words that you don't want to Titlecase
    var dontChangeTheseWords = ["de", "la"];
    if(dontChangeTheseWords.indexOf(str) > -1)
    {
        return str;
    }

    return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142