正则表达式最多匹配 2 个完整单词和包含该字符的下一个单词

Regex to match up to 2 full words and the next word containing the character

我开发了以下用于搜索字段的正则表达式。
目标是用它来匹配最多 2 个单词,然后是带有字符的完整单词以及后面的所有内容:

/^
    .*?                 # match anything before, as few times as possible
    (
        (?: 
            [^\s]+\s*   # anything followed by whitespace
        ){1,2}          # match once or twice
        \s*?            # match whitespaces that may be left behind, just in case
        [^\s]*?         # match the beginning of the word, if exists
    )?  
    (foo|bar)           # search term(s)
    ([^\s]*\s*.*)       # whatever is after, with whitespace, if it is the end of the word
$/xi

问题是它并不总是正确匹配。
几个例子,在搜索 "a":

Fantastic drinks and amazing cakes

Expected match:
 = F
 = a
 = ntastic drinks and amazing cakes

Result:
 = Fantastic drinks (space)
 = a
 = nd amazing cakes

-----------------------------------------

Drinks and party!

Expected match:
 = Drinks (space)
 = a
 = nd party!

Result:
 = Drinks and p
 = a
 = rty!

------------------------------------------

Drinks will be served at the caffetary in 5 minutes

Expected match:
 = be served (space)
 = a
 = t the caffetary in 5 minutes

Result (matches correctly):
 = be served (space)
 = a
 = t the caffetary in 5 minutes

您可以在 https://regex101.com/r/cI7gZ3/1 上进行试验,包括单元测试。

这不起作用的方式很奇怪,超出了我的描述范围。但是,我的猜测是,这是更喜欢在搜索词 之前 有 1-2 个词的匹配项。

您认为这里可能有什么问题?您认为是什么导致了这些问题?

我建议在

中使用\S+{1,2}的懒惰版本
(?: 
    \S+?\s* # anything followed by whitespace
){1,2}?

并删除 [^\s]*? # match the beginning of the word, if exists 部分。

updated regex demo

^
  .*? # match anything before, as few times as possible
  (
    (?: 
      \S*?\s* # anything followed by whitespace
    ){1,2}?
    \s* # just in case there's whitespace
  )?
  (a) # search term(s)
  (\S*\s*.*) # whatever is after, without whitespace if it is the end of the word
$