javascript - JS/正则表达式 : Remove a substring from end of string with optional comma and spaces in front and dot behind

标签 javascript regex string

我正在尝试使用正则表达式从字符串末尾删除字符串et al。可选的是,末尾可以有一个点,并且在此子字符串前面可以有一个逗号和/或多个空格:

https://regex101.com/r/aBw7xy/1

string.replace(/.*(,\s?\s*et\s+al.*)$/g, '')

但这对于这些输入示例的第一行不起作用:

name firstname secondname et al
name firstname secondname, et al
name firstname secondname,et al
name firstname secondname, et al.
name     firstname     secondname,   et   al.
name firstn. sec., et al.
name f s, et al.

我想去掉这个子字符串前面的每个et al和空格/逗号。

结果应该是:

name firstname secondname
name firstname secondname
name firstname secondname
name firstname secondname
name     firstname     secondname
name firstn. sec.
name f s

如果该组完全匹配,我该如何首先测试?

最佳答案

试试这个:

(,?\s*et\s+al.*)$

你的:.(,\s?\set\s+al.)$ . 将匹配您希望保留的所有内容, \s?不再需要了,因为你有一个\s*

关于javascript - JS/正则表达式 : Remove a substring from end of string with optional comma and spaces in front and dot behind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48106984/

相关文章:

Javascript - 正则表达式仅捕获前 3 个匹配项

javascript - 如何防止 grunt uglify 覆盖目标文件夹

javascript - 2 个服务的 2 个 http 服务器在 Node.js Bluemix 应用程序中崩溃

javascript - 将鼠标悬停在段落上

java - 在 O(1) 中反转 Java 中的字符串?

python - Pandas 数据框中的条件分组

无法初始化字符串数组并使用它们

javascript - jQuery 获取表中 td 的值

regex - 正则表达式:持续时间(小时,分钟),以 Dart 表示

java - 如何使用 JAVA 从大字符串中提取路径?