我需要提取中间名和姓氏
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:
尝试过
function getSNames(input) {
const names = input.match(/(?<!\[)(?<=\s)\w+(?=\s)/g);
return names ? names.join(' ') : '';
}
最佳答案
你可以使用
const names = input.match(/(?<!\(\p{L}+\s+|\p{L})\p{L}+(?:\s+\p{L}+)*(?=\s*<)/gu)
参见 regex demo . u
标志启用 Unicode 类别类。
图案细节
-
(?<!\(\p{L}+\s+|\p{L})
- 紧靠左边,不可能有(
后跟 1+ 个字母,然后是一个或多个空格,或者只是一个字母(这用作 Unicode 单词边界) -
\p{L}+
- 一个或多个字母 -
(?:\s+\p{L}+)*
- 零次或多次出现 1+ 个空格,然后是 1+ 个字母 -
(?=\s*<)
- 紧靠右边,必须有 0+ 个空格,然后是<
.
关于javascript - 使用 Javascript 的正则表达式从字符串中提取中间名和姓氏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64555618/