Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract names and email using regex in Javascript

I have a string with type, the expected results are

input = "[Peter Jane Minesotta <[email protected]>]"

output

Fname = "Peter"
SecondAndRemainingNames = "Jane Minesotta"
email = "[email protected]"

input = "[Peter  <[email protected]>]"

output

    Fname = "Peter"
    SecondAndRemainingNames = ""
    email = "[email protected]

I need to extract using regex

I have tried with

input.match(/\w/gim)
like image 796
Code Guy Avatar asked Dec 31 '22 19:12

Code Guy


2 Answers

You can use

const rx = /\[(\S+)(?:\s+(.*?))?\s+<([^<>]+)>]/
const strings = ['[Peter Jane Minesotta <[email protected]>]','[Peter  <[email protected]>]'];
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.
like image 91
Wiktor Stribiżew Avatar answered Jan 02 '23 09:01

Wiktor Stribiżew


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 <[email protected]>]"

const input2 = "[Peter  <[email protected]>]"

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);
like image 31
Greedo Avatar answered Jan 02 '23 07:01

Greedo