最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parsing out Salutation & First Name from Full Name field - Stack Overflow

programmeradmin2浏览0评论

I have a string that contains Full Name.

The format of the full name may or may not have the salutation. Also there may or may not be a period after the salutation as well (could display as Mr. or Mr). For example, I could receive:

  • "Mrs. Ella Anderson"
  • "Ella Anderson"
  • "Miss Jennifer Sply"
  • "Mr. Dan Johnson"
  • "Damien Hearst"

My goal is to remove the salutation from the Full Name string. Once the salutation is removed, I want to parse out the First Name from the Full Name. I am kinda new to regex, but I do understand how to parse out the First Name. The one part I am just not sure how to do is get rid of the salutation.

var string = "Ella Anderson"
var first = string.replace(/\s.*$/, "").toUpperCase().trim();

I have a string that contains Full Name.

The format of the full name may or may not have the salutation. Also there may or may not be a period after the salutation as well (could display as Mr. or Mr). For example, I could receive:

  • "Mrs. Ella Anderson"
  • "Ella Anderson"
  • "Miss Jennifer Sply"
  • "Mr. Dan Johnson"
  • "Damien Hearst"

My goal is to remove the salutation from the Full Name string. Once the salutation is removed, I want to parse out the First Name from the Full Name. I am kinda new to regex, but I do understand how to parse out the First Name. The one part I am just not sure how to do is get rid of the salutation.

var string = "Ella Anderson"
var first = string.replace(/\s.*$/, "").toUpperCase().trim();
Share Improve this question asked Nov 25, 2015 at 7:57 RachelRachel 312 bronze badges 3
  • Why not check the first name against a dictionary of all possible salutations? They aren't much I guess ([Mr, Mrs, Dr, Miss, ...]). If it's equal to one of those, it's most probably not the person's name. Other than that, you'd need some kind of delimiter. – Frax Commented Nov 25, 2015 at 8:02
  • 1 "Missy Peterson" - Oh, the salutation mist be "Miss". "Hello, Miss y Peterson"! Also, how would you determine with 3 names if the middle name belongs with the surname, or the first name? – Cerbrus Commented Nov 25, 2015 at 8:05
  • No, just split the string on the white space. You'd get "Missy". Check it against the dictionary of salutations. Missy doesn't exist, so it's the first name. – Frax Commented Nov 25, 2015 at 8:09
Add a ment  | 

5 Answers 5

Reset to default 4

This regex should work.

var regex = /(Mr|MR|Ms|Miss|Mrs|Dr|Sir)(\.?)\s/,
    fullNames = ["Mrs. Ella Anderson", "Ella Anderson", "Miss Jennifer Sply", "Mr. Dan Johnson", "Damien Hearst"];

var names = fullNames.map(function(name) {
  var match = regex.exec(name),
      n = "";
  (match !== null) ? n = name.replace(match[0], "") : n = name;
  return n;
});

console.log(names);

The problem is that the full name is in a string in the first place. If at all possible, you should change that to just use separate fields.

There's no telling what users will enter in a text box. Nor is it reliably possible to determine what part of the remaining name is the first name, and what part is the surname.

If the input data is separated properly, you won't have to figure out what is what, any more.

So, if possible, change the way the name is entered to something like:

<select name="select">
  <option>Miss</option> 
  <option>Mrs</option>
  <option>Mr</option>
  <option>etc...</option>
</select>
<input placeholder="First name" />
<input placeholder="Surname" />

You can use this regexp: /((Mrs|Mr|Miss)\.? )?([^ ]*) ?([^ ]*)/

Examples:

var regex = /((Mrs|Mr|Miss)\.? )?([^ ]*) ?([^ ]*)/;
regex.exec('Mrs. Ella Anderson') == ["Mrs. Ella Anderson", "Mrs. ", "Mrs", "Ella", "Anderson"];
regex.exec("Ella Anderson") == ["Ella Anderson", undefined, undefined, "Ella", "Anderson"];
regex.exec("Miss Jennifer Sply") == ["Miss Jennifer Sply", "Miss ", "Miss", "Jennifer", "Sply"];
regex.exec("Mr. Dan Johnson") == ["Mr. Dan Johnson", "Mr. ", "Mr", "Dan", "Johnson"];
regex.exec("Damien Hearst") == ["Damien Hearst", undefined, undefined, "Damien", "Hearst"];
regex.exec("Missy Jennifer") == ["Missy Jennifer", undefined, undefined, "Missy", "Jennifer"];

If you want the first name and the last name, you just have to look at the last two values of the array.

Of course, this regexp will not work with something like `Mr. John Smith Junior. If you want something generic, don't use a regexp.

It's a pretty plicated regex:

/^(?:(Miss|M[rs]{1,2})\.?\s+)?(\S+)\s+(\S+)$/

Then if you want middle names or initials it gets a little trickier things like jr. or sr. - It's mostly all doable. There's some question about how to deal with hyphenates.

You can use this regexp:^[ \t]*(?<title>(Shri|Leu|DR|mrs|SMT|Major|Gen){1,10}(\.|,))?\s*(?<LstName>[A-Z][a-z-']{2,20}),? +(?<FstName>[A-Z,a-z]+)*[ \t]*[^\n]*

Tested on the following Test data: Major. Amator Gary L Mrs. Grundy Ronald Dr. Domsky Alan Shri. Worden Scott Allen Rodriguez Howard W NEHME ALLEN RODRIGUEZ CHARLES G VERGARA WILLIAM F J EVELYN J Leu. GLICK, JACOB L. SMT. Taylor-garcia Dottielou

发布评论

评论列表(0)

  1. 暂无评论