从文本字符串中提取变量 Javascript

Extract variables from text string Javascript

我创建了一个 Google 幻灯片,其中包含很多变量。我希望能够立即将其中的所有变量拉到 Google 表格中。我正在使用应用程序脚本从幻灯片中的所有形状中获取所有文本作为字符串。

我有包含文本和变量的字符串。

The text contains {{variable1}} and {{variable2}} and also some more {{variable3}}

所需的输出是获取包含所有变量名称的数组。

[variable1, variable2, variable3]

谢谢!

const input = "The text contains {{variable1}} and {{variable2}} and also some more {{variable3}}"
const regex = /{{(\w+)}}/g
const matches = [...input.matchAll(regex)]
console.log(matches.map(([, x]) => x))

即使您不知道正则表达式,您也可以使用 split()。虽然它很 hacky

const input = "The text contains {{variable1}} and {{variable2}} and also some more {{variable3}}"

console.log(input.split('{{').slice(1).map(x => x.split('}}')[0]))