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.
- 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
3 Answers
Reset to default 4I'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
版权声明:本文标题:javascript - Splitting Name into last name, first name, middle initial - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741650671a2390456.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论