Nodejs String Title Case toTitleCase()

Here you can find the source of toTitleCase()

Method Source Code

// Title Case/*from   w  w  w.  ja v a2s. c  o  m*/
String.prototype.toTitleCase = function() {
    var i, j, str, lowers, uppers;
    str = this.replace(/([^\W_]+[^\s-]*) */g, function(txt) {
    return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
    
    // Certain minor words should be left lowercase unless 
    // they are the first or last words in the string
    lowers = ['A', 'An', 'The', 'And', 'But', 'Or', 'For', 'Nor', 'As', 'At', 
    'By', 'For', 'From', 'In', 'Into', 'Near', 'Of', 'On', 'Onto', 'To', 'With'];
    for (i = 0, j = lowers.length; i < j; i++)
    str = str.replace(new RegExp('\\s' + lowers[i] + '\\s', 'g'), 
        function(txt) {
            return txt.toLowerCase();
        });
    
    // Certain words such as initialisms or acronyms should be left uppercase
    uppers = ['Id', 'Tv'];
    for (i = 0, j = uppers.length; i < j; i++)
    str = str.replace(new RegExp('\\b' + uppers[i] + '\\b', 'g'), 
        uppers[i].toUpperCase());
    
    return str;
};

Related

  1. toTitleCase()
    String.prototype.toTitleCase = function() {
        return this.replace(/\w\S*/g, function(txt) {
            if (txt)
                return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
            else
                return this;
        });
    
  2. toTitleCase()
    String.prototype.toTitleCase = function() {
      return this.replace(/\w\S*/g, function(txt) {
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
      });
    };
    
  3. toTitleCase()
    String.prototype.toTitleCase = function() {
      return this.replace(/([\w&`'?"?@:\/\{\(\[<>_]+-? *)/g,
      function(match, p1, index, title) {
        if (index > 0 && title.charAt(index - 2) !== ":" && match.search(/^(a(nd?|s|t)?|b(ut|y)|en|for|i[fn]|o[fnr]|t(he|o)|vs?\.?|via)[ \-]/i) > -1) return match.toLowerCase();
        if (title.substring(index - 1, index + 1).search(/['"_{(\[]/) > -1) return match.charAt(0) + match.charAt(1).toUpperCase() + match.substr(2);
        if (match.substr(1).search(/[A-Z]+|&|[\w]+[._][\w]+/) > -1 || title.substring(index - 1, index + 1).search(/[\])}]/) > -1) return match;
        return match.charAt(0).toUpperCase() + match.substr(1);
      });
    };
    ...
    
  4. toTitleCase()
    String.prototype.toTitleCase = function() {
      var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;
      return this.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title){
        if (index > 0 && index + match.length !== title.length &&
          match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
          (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
          title.charAt(index - 1).search(/[^\s-]/) < 0) {
          return match.toLowerCase();
        if (match.substr(1).search(/[A-Z]|\../) > -1) {
          return match;
        return match.charAt(0).toUpperCase() + match.substr(1);
      });
    };
    
  5. toTitleCase()
    String.prototype.toTitleCase = function() {
        var i, str, lowers, uppers;
        str = this.replace(/([^\W_]+[^\s-]*) */g, function(txt) {
            return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
        });
        lowers = ['A', 'An', 'The', 'And', 'But', 'Or', 'For', 'Nor', 'As', 'At', 
        'By', 'For', 'From', 'In', 'Into', 'Near', 'Of', 'On', 'Onto', 'To', 'With'];
        for (i = 0; i < lowers.length; i++)
            str = str.replace(new RegExp('\\s' + lowers[i] + '\\s', 'g'), 
    ...
    
  6. toTitleCase()
    String.prototype.toTitleCase = function() {
        var i, j, str, lowers, uppers;
        str = this.replace(/([^\W_]+[^\s-]*) */g, function(txt) {
            return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
        });
        lowers = ['A', 'An', 'The', 'And', 'But', 'Or', 'For', 'Nor', 'As', 'At',
            'By', 'For', 'From', 'In', 'Into', 'Near', 'Of', 'On', 'Onto', 'To', 'With'];
        for (i = 0, j = lowers.length; i < j; i++)
            str = str.replace(new RegExp('\\s' + lowers[i] + '\\s', 'g'),
    ...
    
  7. toTitleCase()
    String.prototype.toTitleCase = function() {
        return this.replace(/([\w&`'??"?.@:\/\{\(\[<>_]+-? *)/g, function(match, p1, index, title) {
            if (index > 0 && title.charAt(index - 2) !== ":" &&
              match.search(/^(a(nd?|s|t)?|b(ut|y)|en|for|i[fn]|o[fnr]|t(he|o)|vs?\.?|via)[ \-]/i) > -1)
                return match.toLowerCase();
            if (title.substring(index - 1, index + 1).search(/['"_{(\[]/) > -1)
                return match.charAt(0) + match.charAt(1).toUpperCase() + match.substr(2);
            if (match.substr(1).search(/[A-Z]+|&|[\w]+[._][\w]+/) > -1 || 
              title.substring(index - 1, index + 1).search(/[\])}]/) > -1)
    ...
    
  8. toTitleCase()
    String.prototype.toTitleCase = function() {
      return this.replace(/(^|\s)[a-z]/g, function($1){return $1.toUpperCase();});
    };
    String.prototype.numberValue = function() {
      return Number(this.replace(/[^0-9\.\-]+/, ""));
    };
    String.htmlEncodeMap = {
      "&": "&amp;",
      "'": "&#39;",
    ...
    
  9. toTitleCase()
    String.prototype.toTitleCase = function() {
      return this.replace(/([\w&`'??:\/\{\(\[<>_]+-? *)/g, function(match, p1, index, title) {
        if (index > 0 && title.charAt(index - 2) !== ":" && match.search(/^(a(nd?|s|t)?|b(ut|y)|en|for|i[fn]|o[fnr]|t(he|o)|vs?\.?|via)[ \-]/i) > -1)
          return match.toLowerCase();
        if (title.substring(index - 1, index + 1).search(/['"_{(\[]/) > -1)
          return match.charAt(0) + match.charAt(1).toUpperCase() + match.substr(2);
        if (match.substr(1).search(/[A-Z]+|&|[\w]+[._][\w]+/) > -1 || title.substring(index - 1, index + 1).search(/[\])}]/) > -1)
          return match;
        return match.charAt(0).toUpperCase() + match.substr(1);
    ...