This seems to work:
var convertName = function(name){
var pattern=/^(.*?[a-z][A-Z])(.*)$/g;
if(pattern.test(name)){
return name.replace(pattern,function(t,a,b){
return a+b.toUpperCase();
});
}
else{
return name.toUpperCase();
}
};
It basically looks for the first upper-case letter after the first lower-case letter, separates that first part from the rest and makes the rest upper-case. This only happens, if such a pattern is found. Otherwise it simply returns the name in upper-case.
Usage
convertName('McDonald'); // McDONALD
convertName('McDowell'); // McDOWELL
convertName('McIntosh'); // McINTOSH
convertName('iPhone'); // iPHONE
convertName('Smith'); // SMITH
Replacing multiple instances
The simplest way is by matching every group of letters and putting that into the function. You can use:
"Word, test, “words”, McIntosh is a name, just like Herbert-McIntosh. So much upper-case.".replace(/(\w+)/g,function(t,w){
return convertName(w);
});
// "WORD, TEST, “WORDS”, McINTOSH IS A NAME, JUST LIKE HERBERT-McINTOSH. SO MUCH UPPER-CASE."
// Prefix after a dash ^^^