javascript lowercase string except first letter of every word if there are '' 1

javascript lowercase string except first letter of every word if there are ”

var text = "foo bar 'loo' zoo moo";
text = text.toLowerCase().split(' ').map((s) => { if(s.charAt(0)!=="'") return s.charAt(0).toUpperCase() + s.substring(1)
      else return s.charAt(0)+s.charAt(1).toUpperCase() + s.substring(2)}).join(' ');
//text = "Foo Bar 'Loo' Zoo Moo"

Here is what the above code is Doing:
1. First, we convert the string to lowercase.
2. Then, we split the string into an array of words.
3. Next, we map over the array of words.
4. If the first character of the word is not an apostrophe, we capitalize the first letter of the word.
5. If the first character of the word is an apostrophe, we capitalize the second letter of the word.
6. Finally, we join the array of words back into a string.

Similar Posts