Extract names and email using regex in Javascript

Viewed 182

I have a string with type, the expected results are

input = "[Peter Jane Minesotta <pet.j.minn@mnu.al.edu>]"

output

Fname = "Peter"
SecondAndRemainingNames = "Jane Minesotta"
email = "pet.j.minn@mnu.al.edu"

input = "[Peter  <pet.j.minn@mnu.al.edu>]"

output

    Fname = "Peter"
    SecondAndRemainingNames = ""
    email = "pet.j.minn@mnu.al.edu

I need to extract using regex

I have tried with

input.match(/\w/gim)
4 Answers

You can use

const rx = /\[(\S+)(?:\s+(.*?))?\s+<([^<>]+)>]/
const strings = ['[Peter Jane Minesotta <pet.j.minn@mnu.al.edu>]','[Peter  <pet.j.minn@mnu.al.edu>]'];
for (const s of strings) {
  const [_, Fname, SecondAndRemainingNames, email] = s.match(rx);
  console.log([Fname, SecondAndRemainingNames, email]);
}

See the regex demo.

Details

  • \[ - a [ char
  • (\S+) - Group 1: one or more non-whitespace chars (to stay within [...], you may use [^\s[\]]+ instead)
  • (?:\s+(.*?))? - an optional string of 1+ whitespaces followed with Group 2 capturing any zero or more chars other than line break chars as few as possible (replace .*? with [^[\]]*? if you want to stay within [...])
  • \s+ - one or more whitespaces
  • <([^<>]+)> - >, Group 3: one or more chars other than < and >, then >
  • ] - a ] char.

You can use 3 different regex in order to simplify the problem. Also, you can rely on the structure of the string:

const input1 = "[Peter Jane Minesotta <pet.j.minn@mnu.al.edu>]"

const input2 = "[Peter  <pet.j.minn@mnu.al.edu>]"

function getFName(input) {
  const name =  input.match(/(?<=\[)\w+/);
  return name ? name[0] : '';
}

function getSNames(input) {
  const names =  input.match(/(?<!\[)(?<=\s)\w+(?=\s)/g);
  return names ? names.join(' ') : '';
}

function getEmail(input) {
  const mail =  input.match(/(?<=<)(?:\w|\.|@)+(?=>])/);
  return mail ? mail[0] : '';
}

const x = {
  name: getFName(input1),
  otherNames: getSNames(input1),
  mail: getEmail(input1)
};

console.log(x);


const y = {
  name: getFName(input2),
  otherNames: getSNames(input2),
  mail: getEmail(input2)
};

console.log(y);

This should give you what you want...

^\[(\w+)\s(?:((?:\w+\s?)*)\s)?<(.+)>\]$
  1. The first group (\w+) would capture the First Word (stops as soon as it finds space) which in your case would be the firstName

  2. The second group (?:((?:\w+\s?)*)\s)? would capture everything that between the last space (after firstName) and first occurrence of < which you want to save in SecondAndRemainingNames. Note: the ? at the end of this group makes occurrence of this pattern optional which is what you want as indicated by your 2nd example..

  3. Finally, the last group would capture everything that's between < and > which for you would be email

I've tested this pattern with both of your sample inputs and it's working as expected .. :)

This works fine:

var all = input.match(/(^\[\w+)|(\w+ )+|<.+>/gi);
var Fname = ""
var SecondAndRemainingNames = ""
var email = ""
if (all.length == 3) {
    Fname = all[0];
    SecondAndRemainingNames = all[1];
    email = all[2];
} else if (all.length == 2) {
    Fname = all[0];
    email = all[1];
}
Fname = Fname.substring(1);
if (SecondAndRemainingNames != "") {
    SecondAndRemainingNames = SecondAndRemainingNames.trim();
}
email = email.substring(1).slice(0, -1);
Related