1

I need to Capitalizing first letter of each word in a string using Javascript but ignore if the word is article/preposition.

var string = "at the drop of a hat";

should return

"At the Drop of a Hat"

Please see here that - 1. Every 1st letter of each word is in uppercase 2. 'the' and 'a' being articles were not capitalized 3. 'of' being a preposition was not capitalized 4. 'at' was capitalized even though it is a preposition as it is start of a sentence.

I could do #1 by

string.toLowerCase().replace(/(^[a-z])|(\s[a-z])|([-,:!?\<">';+‘@`€$*&.\\/][a-z])|(\s[а-яё])|(^[а-яё])/g,function(f){return f.toLocaleUpperCase();});

But, this return "At the Drop Of A Hat".

How can I ignore capitalizing articles and prepositions if they're not occurring as first word in a given sentence (i.e. #2,#3 and #4 above)?

aks
  • 458
  • 1
  • 5
  • 16

0 Answers0