简体   繁体   中英

Extract the middle names and last name from the string using regex of Javascript

I need to extract the middle and last names

Elev: 7EBB49 (Dan Greg Järgenstedt <dan.greg.smith@manu.al.edu>)
Expected: Greg Järgenstedt

Elev: 6EBB49 (Dan Järgenstedt <dan.greg.smith@manu.al.edu>)
Expected: Järgenstedt

Elev: 6EBB49 (Järgenstedt <dan.greg.smith@manu.al.edu>)
Expected: Järgenstedt

Elev: 6EBB49 (<dan.greg.smith@manu.al.edu>)
Expected: 

Tried with

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

You can use

const names = input.match(/(?<!\(\p{L}+\s+|\p{L})\p{L}+(?:\s+\p{L}+)*(?=\s*<)/gu)

See the regex demo . The u flag enables the Unicode category classes.

Pattern details

  • (?<!\\(\\p{L}+\\s+|\\p{L}) - immediately to the left, there cannot be a ( followed with 1+ letters and then one or more whitespaces, or just a letter (this works as a Unicode word boundary)
  • \\p{L}+ - one or more letters
  • (?:\\s+\\p{L}+)* - zero or more occurrences of 1+ whitespaces and then 1+ letters
  • (?=\\s*<) - immediately to the right, there must be 0+ whitespaces and then < .

This way madness lies.

I don't think you can "extract the middle and last names" in general. If all you have is "Name", then you're stuck with it. Whatever rule you come up with, I'll show you a person where it doesn't work. Eg

  • My name is "Peter Valdemar Mørch". first name=Peter, middle name=Valdemar, last name=Mørch
  • I have a friend called Jens Erik Redacted. first name=Jens Erik, no middle name, last name=Redacted. It insults him a little every time a website says "Hi Jens", because he isn't "Jens", he's "Jens Erik".
  • I have another friend called Kristian von Hornsleth. first name=Kristian, no middle name, lastname=von Hornsleth
  • In Chinese, the last name comes before the first name. Eg (I don't know him) "王秀英" is spelled "Wang Xiuying" in English. firstname=Xiuying, no middlename, lastname=Wang ( notice how they're swapped )

What you're doing won't work in the general case, especially not internationally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM