代码之家  ›  专栏  ›  技术社区  ›  Aaron Thomas

用于在同一单词中多次检测模式的正则表达式[重复]

  •  -1
  • Aaron Thomas  · 技术社区  · 2 年前

    什么正则表达式将导致查找所有模式,而不仅仅是第一个模式?例如,在“aaa”中搜索“aa”应该有两个结果(前两个“a”,还有后两个)。

    尝试了以下操作,但没有成功:

    /aa/ # only matches first
    /(aa)/ # only matches first
    /(?<=.)*aa/ # positive lookbehind, only matches first
    
    1 回复  |  直到 2 年前
        1
  •  0
  •   Christian Legge    2 年前

    你可以使用积极的前瞻性。

    /(?=(aa))/ => "aaa" # two matches, at positions 0 and 1