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

牧云@^-^@ 提交于 2019-12-01 12:56:19

First of all, you need to use \\ before . in the GAS regex declaration, as the literal backslash forms a regex escape sequence.

Now, it seems that GAS non-capturing group implementation is buggy.

If you run your regex in GAS and print the Match object, you will see

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

That means, the non-capturing group got "merged" with the first capturing group skipping the first char.

Here are some more experiments:

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

To fix the issue, you may remove the non-capturing parentheses, as \d = (?:\d).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!