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"
Asked
Active
Viewed 79 times
1 Answers
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
-
It returns "Degas-urb.jardín De Las Avenidas". Results should be "Degas-Urb.Jardín de las Avenidas" – Gabrielius B. Sep 09 '16 at 13:43
-
Because you split on spaces instead of word boundaries – Thomas Ayoub Sep 09 '16 at 13:44