Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Help Capitalizing Scottish Names

    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
    Code:
    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

  • #2
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    Code:
    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();
    }));
    I am still learning English

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,243
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    This seems to work -

    Code:
    <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
    Last edited by Philip M; 01-14-2012 at 03:14 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    damiantaylor (01-14-2012)

  • #4
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    Oops. Sorry, my previous example works as expected only in FF (I tested it in Firebug). =(

    Here is a cross-browser solution:
    Code:
    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();
    }));
    I am still learning English

  • Users who have thanked Amphiluke for this post:

    damiantaylor (01-14-2012)

  • #5
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,243
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    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:-

    Code:
    <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>
    Last edited by Philip M; 01-15-2012 at 05:08 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •