regex Google Apps脚本Extracing捕获组

yqhsw0fo  于 2023-06-07  发布在  Go
关注(0)|答案(2)|浏览(399)

为了最小化可重复的示例,假设我有以下内容:

const REGEX = new RegExp(/[0-9]{4}\s([a-z]{4})/gi);

function myFunction() {
  let text = "Here is a code: 4343 nmbv can I capture and group it?";
  let match = text.match(REGEX);
  Logger.log(match);
}

...

11:02:39 AM Info    [4343 nmbv]

我希望能够使用捕获组“原子化”匹配。假设我先要数字部分。当我尝试添加命名捕获组时,失败了:

const REGEX = new RegExp(/(?P<number>[0-9]{4})\s([a-z]{4})/gi);

function myFunction() {
  let text = "Here is a code: 4343 nmbv can I capture and group it?";
  let match = text.match(REGEX);
  Logger.log(match);
}

...

"Attempted to execute myFunction, but could not save.

我知道在进行替换时可以按顺序引用匿名捕获组。但是,当我试图在正则表达式替换的上下文之外引用匿名捕获组时,可以预见的是,它会失败:

const REGEX = new RegExp(/[0-9]{4})\s([a-z]{4})/gi);

function myFunction() {
  let text = "Here is a code: 4343 nmbv can I capture and group it?";
  let match = text.match(REGEX);
  Logger.log(match);
  Logger.log("$1");
}

...

"Attempted to execute myFunction, but could not save."

如何在Google Apps Script中获取捕获组匹配项?

x9ybnkn6

x9ybnkn61#

首先,ECMAScript 2018中命名的捕获组语法不是(?P<number>[0-9]{4}),而是(?<number>[0-9]{4})。你需要

const REGEX = /(?<number>[0-9]{4})\s([a-z]{4})/gi;

然后,为了获得所有匹配,您需要String#matchAll,它也将保留组。String#match省略所有组。
然后,一旦获得匹配,就需要访问match.groups.<group_name>以获得命名组值。
你可以用

function myFunction123() {
  let text = "Here is a code: 4343 nmbv can I capture and group it?";
  let match = text.matchAll(REGEX);
  Logger.log(Array.from(match, x => [x.groups.number, x[1], x[2]]));
}
// => 11:21:45 AM   Info    [[4343, 4343, nmbv]]

请注意,命名的捕获组也接收数字索引。由于(?<number>[0-9]{4})是正则表达式中的第一个组,它的ID为1,因此x.groups.number的值与x[1]的值相同。

0ve6wy6x

0ve6wy6x2#

如果你希望在一个字符串中有多个匹配项,可以使用以下命令:

let regex = /(?<number>[0-9]{4})\s([a-z]{4})/gi;
let text = "Here is a code: 4343 nmbv can I capture and group it?";

let match;
while ((match = regex.exec(text)) !== null) {
  let { number } = match.groups;
  Logger.log(`NUMBER: ${number}`);
  Logger.log(`GROUPS: ${match.groups}`);
}

正如@Wiktor所述,JavaScript正则表达式中的命名组通过语法(?<GROUP_NAME>PATTERN)声明。

相关问题