JavaScript Convert Names to Uppercase, Except Mc/Mac/etc

uppercase mc names
php lowercase
php uppercase first letter of each word
last name all caps
how to capitalize all words in php
ucfirst not working
php get first letter of each word
last names with two capital letters

Everything I can find by searching is people wanting to convert to sentence/title case from lower/upper/random case. That's the opposite of my problem.

What I have is already correct, I want to convert it to uppercase except for the "c" or "ac" etc. So McDonald becomes McDONALD, MacDonald becomes MacDONALD, etc.

Probably the best way is separating out the lower-case letters that occur between two upper-case letters, either before or after running toUpperCase(), but my brain is fried at the moment so I'm not sure how to go about it.

It's for an After Effects expression, controlling the display so I can have sentence case in one composition and upper case in another, from the same source layer. So I know input will be perfect.


You can try something like this:

const input = "MacDonald";

const matches = input.match(/([A-Z][a-z]*)/g);

const output = matches.length > 1 ? 
  matches.reduce((acc, match, index) => {
    if (index === 0) {
      return match;
    }
    return acc + match.toUpperCase();
  }) :
  input.toUpperCase();

First we take the input apart by matching it against a simple regular expression. The match method in the example will return ["Mac","Donald"].

Then, if there is only one match, we return it in uppercase.

In case of multiple matches, we construct the result by concatenating uppercase parts except for the first part.

Here's a version for a whole sentence:

const input = "Old MacDonald is a fine man.";

const output = input
  .split(/\s/)
  .map(word => {

    const matches = word.match(/([A-Z][a-z]*)/g);

    if (!matches || matches.length === 1) {
        return word.toUpperCase();
    }

    return matches.reduce((acc, match, index) => {
      return index === 0 ? match : acc + match.toUpperCase();
    });
  })
  .join(' ');

// output == "OLD MacDONALD IS A FINE MAN."

Replace 3rd character in string with uppercase, For a better experience, please enable JavaScript in your browser before proceeding. Think about names like O'Reilly, Mac Donald, Mc-Fisher etc. I'm sure 'It is not to be altered or distributed, 'except as part of an application. vbProperCase 3 Converts the first letter to every word to uppercase. Definition and Usage. The toUpperCase() method converts a string to uppercase letters. Note: The toUpperCase() method does not change the original string. Tip: Use the toLowerCase() method to convert a string to lowercase letters.


Sami Hult's answer covers most of the bases, but unfortunately refuses to work in After Effects due to syntax issues and map() and reduce() not being supported, and I wanted to make one small tweak, all-capsing only the last portion rather than all but the first (to account for a possible double prefix).

So based on that code, I came up with this:

function str_uppercase(str) {
    str = str.split(/\s/);
    var output = [];

    for (i = 0; i < str.length; i++) {
        var word = str[i];
        var matches = word.match(/([A-Z][a-z]*)/g);

        if (!matches || matches.length === 1) {
            word = word.toUpperCase();
        } else {
            var x = matches.length - 1;
            matches[x] = matches[x].toUpperCase();
            word = matches.join('');
        }

        output.push(word);
    }

    return output.join(' ');
}

console.log(str_uppercase('Old MacMcDonald Had a farm'));
// => OLD MacMcDONALD HAD A FARM

Scientific American, 203.004 Moulding machine. etc , G. S. Roberts . . . . . . . . .. 213.272 Mop. H. L. l"​ranklin . . . . . . . . . . . . . . . . . . . . . . . . .. 213.165 Motion transmitting and converting. T. H. Mc.'ray . J. S. Birch . 6.586 Cartridges and percussion caps. of '​upermnki . describing the Latest and est Processes and Mac ines in use. with Statistics. The toUpperCase () method returns the value of the string converted to uppercase. This method does not affect the value of the string itself since JavaScript strings are immutable.


The code below assumes a string prefix to be one capital letter character followed by one or more small letter characters followed by one capital letter character and always at the beginning of the whole word.

The prefix will be retained as it is and the rest will be capitalized.

const input = [
  "McDonald",
  "MacDonald",
  "Mcdonald",
  "mcDonald",
  "mcdonald"
];

// Function for converting to special uppercase
const specialUpperCase = function(item) {
  // Find prefix (one or more lower case characters between upper case character - at the beginning)
  const match = item.match(/^[A-Z][a-z]+[A-Z]/);
  if (match) {
    // If prefix, capitalize only the remaining
    return match[0] + item.substr(match[0].length).toLocaleUpperCase();
  }
  // If no prefix, capitalize the whole string
  return item.toLocaleUpperCase();
};

const output = input.map(specialUpperCase);

console.log(output);

Billboard, If you are having mail addressed to you in our care, look for your name EACH WEEK, Gertrude (Revue) Ayers, M. C. Backwards, Prof, (Comic) Bacon, Sanders Walter or Ginsburg Mac Coleman, Clifford Maddox, Larry Vlaki, Eddie Vlaltin. Wilson, Lewis Witham, Eugene H. Woodson Family (acrobatic turn) Wright Jr.. • Names like O’Connor would turn into O’connor. For most of these you could write custom rules to deal with it, however, this still has issues with Acronyms as before and you get a new issue: • Adding in a rule to fix names with Mac such as MacDonald, would the break names such as Macy turning it into MacY.


The easiest solution would probably be to keep a list of prefixes and test if the word starts with one of these:

//Prefixes to look for
var prefixToKeep = [
    "Mac",
    "Mc"
];
//Selective uppercase function
function selectiveUpperCase(text) {
    //Find words by wordBoundaries
    return text.replace(/\b\w+\b/gim, function (word) {
        //Test for prefixes
        for (var prefixToKeepIndex = 0; prefixToKeepIndex < prefixToKeep.length; prefixToKeepIndex++) {
            var prefix = prefixToKeep[prefixToKeepIndex];
            if (word.indexOf(prefix) === 0) {
                //prefix matches. Return prefix as is + rest of the word in uppercase
                return word.slice(0, prefix.length) + word.slice(prefix.length).toUpperCase();
            }
        }
        //No prefix found, return word as uppercase
        return word.toUpperCase();
    });
}
//TEST
var text = "Old MacDonald had a farm\nE-I-E-I-O\nAnd on this farm he had a cow\nE-I-E-I-O\nWith a moo-moo here\nAnd a moo-moo there\nHere a moo, there a moo\nEverywhere a moo-moo\nOld MacDonald had a farm\nE-I-E-I-O ";
console.log(selectiveUpperCase(text));

ucfirst - Manual, Here's a function to capitalize segments of a name, and put the rest into lower case. This is what I use for converting strings to sentence case: Mac OS 10.5.​1 possibility that if the second part of the double-barreled name starts with "mc",​  Switch between uppercase and lowercase in Word on Mac. While the above steps work for most applications on your Mac, Microsoft Word is a little different. You can use a handy keyboard shortcut to quickly change letter cases in Word. 1) Select the text, whether a single word or entire document.


ES6 version with RegEx, you can try below function replaceStr()

const replaceStr = str => str.replace(/(^[A-Z])([a-z]{1,2})(.+)/,
    (_, p1, p2, p3) => p1.toUpperCase() + p2 + p3.toUpperCase());

ucwords - Manual, ucwords — Uppercase the first character of each word in a string My quick and dirty ucname (Upper Case Name) function. Exceptions all in upper case are any words you don't want converted to title Here is a function to capitalize a last name, accounting for hyphens, apostrophes, "Mc" and "Mac": The function does handle any last name beginning with Mc or Mac or with apostrophe or dash (because for regex anything after apostrophe or dash is a new word). So what my function does is change "mcdonald" to "McDonald" or "macintyre" to "MacIntyre", or "o'reilly" to "O'Reilly". I have tested it and it is functional.


Text Capitalization Rules, First letter after a space ("Mac donald" becomes "Mac Donald.") First letter after “Mc” if the entire name is lowercase or uppercase ("MCDONALD" and  An exception is in names where the actual surname following the prefix begins with a vowel, as in McIntyre: in these cases Mc is pronounced Mac, and also becomes the stressed syllable. (Everyone I know with this type of name identifies as Scots-Irish; I am not sure about native Irish names, but I believe it's not so common: O'– meaning pretty


Javascript for Title Case, I am currently using the following Javascript for Title Case, but I would like to include exceptions (i.e. last names starting with Mc, force uppercase of State abbreviations, and abbreviations for North East - NE, etc.). ---Beginning of JavaScript - Entered Which is converted to: John Mcelroy 123 Any Street Ne There are different ways in which we can convert the string to uppercase in javascript. We will see how to convert single, multiple or all the characters of the string to the uppercase. Using string’s toUpperCase() method. toUpperCase() method only converts the alphabets to the uppercase.


Camel case, Camel case is the practice of writing phrases such that each word or abbreviation in the middle Camel case is often used for variable names in computer programming. medial capitals are usually only found in Scottish or Irish "Mac-" or "Mc-" names, record fields, methods, procedures, functions, subroutines, types, etc. -- If word length is less than or equal to the UPPERCASE word length, convert to upper case. -- e.g. you can specify a @UCASEWordLength of 3 to automatically UPPERCASE all 3 letter words. set @Word = case when len(@Word) <= @UCASEWordLength then UPPER(@Word) else @Word end -- Check word against user exceptions list.