这个正则表达式删除子字符串有什么问题?

Whats wrong with this regex to remove substring?

正在尝试从地址字符串中删除多余的收件人。在下面的示例中,dba bobs 是要删除的目标字符串。

const NOT_EXTRA_ADDRESSEE = /^(?!.*(attn|co|dba|fka|dept).*\n).*\n/gim;

"bobs burgers dba bobs dinner\n100 Oceanside drive\nnashville, tn 37204"
  .replace(NOT_EXTRA_ADDRESSEE, "");

以上结果:

bobs burgers dba bobs dinner
100 oceanside drive
nashville tn 37204

当想要的是:

bobs burgers
100 oceanside drive
nashville tn 37204

我做错了什么?有时输入在 'dba'.

之前有一个 '\n'

您可以将正则表达式简化为:/\b(attn|co|dba|fka|dept)\b.*/gm

在这里测试:https://regex101.com/r/TOH9VV/2

const regex = /\b(attn|co|dba|fka|dept)\b.*/gm;

// Alternative syntax using RegExp constructor
// const regex = new RegExp('\b(attn|co|dba|fka|dept)\b.*', 'gm')

const str = `bobs burgers dba bobs
100 Oceanside drive
nashville, tn 37204

bobs burgers dba bobs
100 attn Oceanside drive
nashville, tn 37204

bobs burgers dba bobs
100 Oceanside depth drive
nashville, tn fka 37204`;
const subst = ``;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

编辑: 在评论中包含用户 Cary Swoveland 的建议。