Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. String.prototype.toTitleCase = function() {
  2.   var i, j, str, lowers, uppers;
  3.   str = this.replace(/([^\W_]+[^\s-]*) */g, function(txt) {
  4.     return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
  5.   });
  6.  
  7.   // Certain minor words should be left lowercase unless
  8.   // they are the first or last words in the string
  9.   lowers = ['A', 'An', 'The', 'And', 'But', 'Or', 'For', 'Nor', 'As', 'At',
  10.   'By', 'For', 'From', 'In', 'Into', 'Near', 'Of', 'On', 'Onto', 'To', 'With'];
  11.   for (i = 0, j = lowers.length; i < j; i++)
  12.     str = str.replace(new RegExp('\\s' + lowers[i] + '\\s', 'g'),
  13.       function(txt) {
  14.         return txt.toLowerCase();
  15.       });
  16.  
  17.   // Certain words such as initialisms or acronyms should be left uppercase
  18.   uppers = ['Id', 'Tv'];
  19.   for (i = 0, j = uppers.length; i < j; i++)
  20.     str = str.replace(new RegExp('\\b' + uppers[i] + '\\b', 'g'),
  21.       uppers[i].toUpperCase());
  22.  
  23.   return str;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement