...

View Full Version : Help Capitalizing Scottish Names



damiantaylor
01-14-2012, 12:48 PM
I have a function that capitalizes surnames including Scottish surnames, I can't remember where I found it but it has been working perfectly....until recently!

The function will uppercase names that start with mc or mac, so mcdonald will become McDonald and macdonald will become MacDonald.

The problem is if someone has the surname mack, it comes out as MacK.

I think a good fix would be to tell the expression there needs to be at least 2 characters after mc or mac for it to assume it is Scottish but I have no idea how to do it.
The code I have so far is


var temp = s.value.toLowerCase().replace( /\b((m)(a?c))?(\w)/g,
function($1, $2, $3, $4, $5) { if($2){return $3.toUpperCase()+$4+$5.toUpperCase();} return $1.toUpperCase(); });

Can anyone help tweaking what I have please? I'm useless on regular expressions :(

Amphiluke
01-14-2012, 02:02 PM
var s = "mcdonald mack macdonald macleod";
alert(s.replace(/\b(m(a)?c)?(\w)(?=\w)/ig, function($1, $2, $3, $4) {
return ($2) ? "M" + $3 + "c" + $4.toUpperCase() : $4.toUpperCase();
}));

Philip M
01-14-2012, 02:18 PM
This seems to work -


<script type = "text/javascript">

var s = "macdonald mcdonald mack mace mackie macy";
var temp = s.toLowerCase().replace( /\b((m)(a?c))?(\w)/g,
function($1, $2, $3, $4, $5) {
if($2){var x = $3.toUpperCase()+$4+$5.toUpperCase();
x = x.replace(/K$/,"k");
x = x.replace(/E$/,"e");
x = x.replace(/Y$/,"y");
return x;
}
});

alert (temp);

</script>

I understand that the prefix Mac meaning "son of" requires the following proper name to be capitalised - MacDonald, but where the prefix Mac is followed by a person's occupation, e.g. Macnab (son of the abbott), Maccosh (son of the footman), or Mackenzie (son of the fair one) the part after Mac is not capitalised.

Quizmaster: Which classic sparkling wine is named after the region of North-East France where it originated?
Contestant: Jacob's Creek

Amphiluke
01-14-2012, 02:59 PM
Oops. Sorry, my previous example works as expected only in FF (I tested it in Firebug). =(

Here is a cross-browser solution:

var s = "mcdonald mack macdonald macleod";
alert(s.replace(/\b(m(a)?c)?(\w)(?=\w)/ig, function($1, $2, $3, $4) {
return ($2) ? "M" + ($3 || "") + "c" + $4.toUpperCase() : $4.toUpperCase();
}));

damiantaylor
01-14-2012, 03:24 PM
Awesome guys, thanks!

Amphiluke I used your solution although I'm sure Philip M your solution works perfectly too.

I didn't realise Scottish names were so comlpex Philip M :eek:

Philip M
01-15-2012, 04:37 PM
I have looked into this further, and find that the prefix Mac meaning "son of" requires a following proper name to be capitalised - MacDonald / MacLeod, but where the prefix Mac is followed by a father's occupation or physical characteristic (which seems to be more common), e.g. Macnab (son of the abbott), Maccosh (son of the footman), Mackenzie (son of the fair one), Macmillan (son of the bald one), or Macpherson (son of the parson), the part after Mac is not (properly) capitalised. Observation of actual names bears this out.

This suggests that you might be more correct in making a list of Scottish names where the Mac prefix is followed by a proper name, and running through a simple replace sequence:-


<script type = "text/javascript">

var nm = "macdonald";
var first = nm.substring(0,1);
var second = nm.substring(1);
nm = first.toUpperCase() + second.toLowerCase();
alert (nm); //Macdonald
nm = nm.replace(/macdonald/i, "MacDonald"); // and so on.
alert (nm); //MacDonald
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum