Posted By

pmw57 on 12/16/15


Tagged

javascript js case conversion titlecase


Versions (?)

Titlecase


 / Published in: JavaScript
 

A simple function to titlecase a string, ignoring already capitalised words such as NASA and iTunes.

Example:

title = titlecase("NASA is not involved with iTunes"); // "NASA Is Not Involved With iTunes"

Source: https://www.sitepoint.com/community/t/capitalizing-first-letter-of-each-word-in-string/209644/25

  1. function titlecaseLowercaseWords(ignore, p1, p2, p3, p4) {
  2. return p1 + p2.toUpperCase() + p3 + p4;
  3. }
  4. function titlecase(str) {
  5. var wordsRx = /(^|\s)([a-z])([a-z]*)(\s|$)/g;
  6. return str.replace(wordsRx, titlecaseLowercaseWords);
  7. }

Report this snippet  

You need to login to post a comment.