开发者

Javascript Regular Expression to attempt to split name into Title/First Name(s)/Last Name

I want to try and detect the differ开发者_JAVA百科ent parts of a person's name in Javascript, and cut them out so that I can pass them onto something else.

Names can appear in any format - for example:-

miss victoria m j laing

  • Miss Victoria C J Long
  • Bob Smith
  • Fred
  • Mr Davis

I want to try and write something simple, that'll do it's best to guess these and get them right 80% of the time or so (We have some extremely dodgy data)

I'm thinking of something along the lines of using a regex to check whether it has a prefix, then branch off to two places as to whether it has

/^(Dr|Mr|Mrs|Miss|Master|etc).? /

And then cutting the rest of it out using something like

/(\w+ )+(\w+)/

To match last name and other names. Though, I'm unsure on my greedy/ungreedy options here, and whether I can do soemthing to shortcut having all the different paths that might be available. Basically, hoping to find something simple, that does the job in a nice way.

It's also got to be written in Javascript, due to the limitations of the ETL tool I'm using.


Why not split() and just check the resulting parts:

// Split on each space character
var name = "Miss Victoria C J Long".split(" ");

// Check the first part for a title/prefix
if (/^(?:Dr|Mr|Mrs|Miss|Master|etc)\.?$/.test(name[0])) {
    name.shift();
}

// Now you can access each part of the name as an array

console.log(name);
//-> Victoria,C,J,Long

Working Demo: http://jsfiddle.net/AndyE/p9ra4/

Of course, this won't work around those other issues people have mentioned in the comments, but you'd struggle on those issues even more with a single regex.


var title = '';
var first_name = '';
var last_name = '';
var has_title = false;

if (name != null)
{
    var new_name = name.split(" ");

    // Check the first part for a title/prefix
    if (/^(?:Dr|Mr|Mrs|Miss|Master)\.?$/i.test(new_name[0]))
    {
        title = new_name.shift();
        has_title = true;
    }
    if (new_name.length > 1)
    {
        last_name = new_name.pop();
        first_name = new_name.join(" ");
    }
    else if(has_title)
    {
        last_name = new_name.pop();
    }
    else
    {
        first_name = new_name.pop();
    }
}

Adapted from Accepted Answer :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜