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
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.