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

时间:2021-05-13 16:16:14

标签: javascript google-apps-script google-slides

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

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

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

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

[variable1, variable2, variable3]

谢谢!

1 个答案:

答案 0 :(得分:0)

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]))

相关问题