python 和 google 应用程序脚本中正则表达式的区别(后端引擎相关?)

Difference of regex in python and google app script (backend engine related?)

我在 python(3.6,jupyter notebook)和 Google 应用程序脚本中尝试了相同的正则表达式,但似乎 "non-capturing group" 在应用程序脚本中不起作用.

# python script:
import re
text='<a class=""email"" href=""mailto:SOisAwesome@hello.edu"">'
regex='(?:<a class=""email"" href=""mailto:)(.+?@hello\.edu)(?:"">)'
match=re.search(regex,text)
print(match.group(1))
# result is 'SOisAwesome@hello.edu'

// Google app script
function myFunction() {
  string='<a class=""email"" href=""mailto:SOisAwesome@hello.edu"">'
  regex=new RegExp('(?:<a class=""email"" href=""mailto:)(.+?@hello\.edu)(?:"">)')
  Match=regex.exec(string)
  Logger.log(Match[1])
  // result is 'a class=""email"" href=""mailto:SOisAwesome@hello.edu'
}

如果我没记错的话,Google 应用程序脚本中的正则表达式引擎应该支持非捕获组(参考 https://en.wikipedia.org/wiki/Comparison_of_regular_expression_engines,我想我应该看看 "JavaScript (ECMAScript)" 和"Shy groups"?), 谁能解释一下我在这里缺少什么?

提前致谢!

首先,您需要在 GAS 正则表达式声明中的 . 之前使用 \,因为文字反斜杠形成正则表达式转义序列。

现在,GAS 非捕获组的实现似乎有问题。

如果您 运行 GAS 中的正则表达式并打印 Match 对象,您将看到

[18-01-26 08:49:07:198 CET] [<a class=""email"" href=""mailto:SOisAwesome@hello.edu"">, 
a class=""email"" href=""mailto:SOisAwesome@hello.edu, "">]

这意味着,非捕获组获得了 "merged",第一个捕获组跳过了第一个字符

这里还有一些实验:

Logger.log(new RegExp("(?:;\w+):(\d+)").exec(";er:34")); // = null, expected [;er:34, 34]
Logger.log(new RegExp("(?:e\w+):(\d+)").exec(";er:34")); // = null, expected [er:34, 34]
Logger.log(new RegExp("(?:\w+):(\d+)").exec(";er:34"));  // =  [er:34, 34], as expected

要解决此问题,您可以删除非捕获括号,因为 \d = (?:\d)