admin管理员组

文章数量:1299990

I have a user input from a textbox that contains a user's name

input can look like this:

var input = "Doe, John M";

However, input can be a whole lot more plex. like:

var input = "Doe Sr, John M"

or "Doe, John M"

or "Doe, John, M"

or even "Doe Sr, John,M"

What I'd like to do is separate the last name (with the sr or jr) the first name, and then the middle initial.

So, these strings bee :

var input = "Doe#John#M" or "Doe Sr#John#M" or "Doe#John#M"

I've tried this regular expression,

input = input.replace(/\s*,\s*/g, '#'); but this doesn't take into account the last middle initial.

I have a user input from a textbox that contains a user's name

input can look like this:

var input = "Doe, John M";

However, input can be a whole lot more plex. like:

var input = "Doe Sr, John M"

or "Doe, John M"

or "Doe, John, M"

or even "Doe Sr, John,M"

What I'd like to do is separate the last name (with the sr or jr) the first name, and then the middle initial.

So, these strings bee :

var input = "Doe#John#M" or "Doe Sr#John#M" or "Doe#John#M"

I've tried this regular expression,

input = input.replace(/\s*,\s*/g, '#'); but this doesn't take into account the last middle initial.

Share Improve this question asked Apr 4, 2013 at 16:42 JeffJeff 1,8008 gold badges31 silver badges54 bronze badges 4
  • 11 Forget it. Have a field for first, middle if any and last – mplungjan Commented Apr 4, 2013 at 16:43
  • 3 What he said ^^^^^^^^^^^ ! – adeneo Commented Apr 4, 2013 at 16:44
  • 6 You should control how users input their info, not the other way around :). Just give them the input options the way you want it. They'll just have to abide. And they will ;) – user1467267 Commented Apr 4, 2013 at 16:45
  • Please consider the case of having more than one middle initial too. – lrn Commented May 26, 2014 at 6:19
Add a ment  | 

3 Answers 3

Reset to default 4

I'm sure this can probably be done via RegEx but splitting the string into arrays is often faster and a little less plex (IMO). Try this function:

var parseName = function(s) {
  var last = s.split(',')[0];
  var first = s.split(',')[1].split(' ')[1];
  var mi = s[s.length-1];

  return {
    first: first,
    mi: mi,
    last: last
  };    
};

You call it just passing in the name e.g. parseName('Doe, John M') and it returns an object with first, mi, last. I created a jsbin you can try that tests the formats of names you show in your question.

Checkout humanparser on npm.

https://www.npmjs/package/humanparser

Parse a human name string into salutation, first name, middle name, last name, suffix.

Install

npm install humanparser

Usage

var human = require('humanparser');

var fullName = 'Mr. William R. Jenkins, III'
    , attrs = human.parseName(fullName);

console.log(attrs);

//produces the following output

{ saluation: 'Mr.',
  firstName: 'William',
  suffix: 'III',
  lastName: 'Jenkins',
  middleName: 'R.',
  fullName: 'Mr. William R. Jenkins, III' }

The following seems to match your requirements

input = input.replace(/\s*,\s*|\s+(?=\S+$)/g, '#');

本文标签: javascriptSplitting Name into last namefirst namemiddle initialStack Overflow