Search code examples
regexregex-lookaroundsre2

Does Google Sheets Regex engine support lookbehind?


I was under the impression that Sheets supports re2, which does not support lookaround.

Consider he following Sheet's example script. Regex expected to match the word dog either by itself or if preceded by the word cat.

function testRegex(){
  
  var rg = /(?<=(cat|\b))dog/;
  var s = "catdog";
  
  Logger.log("%s: %s",s,rg.test(s));
  
  s = "sdog";
  Logger.log("%s: %s",s,rg.test(s));
  
  s = "dog";
  Logger.log("%s: %s",s,rg.test(s));
  
}

The results are perfect: The results are perfect:

So question is supported or not?


Solution

  • You are using JavaScript in your Google Apps script. GAS now uses the modern JavaScript engine that supports lookbehinds, named capturing groups, s modifier. So, your pattern works well.

    If you use a lookbehind pattern in a REGEXEXTRACT or REGEXREPLACE formula, it will fail as those REGEX~ functions use RE2 regex library. You can't use a lookbehind there.