如何在整个文档中搜索特定单词及其前一个单词(在 java 脚本中)

How to search for specific word in entire document and its previous word (in java script)

我正在为 google 文档做一个补充,在 JavaScript 代码中,我想搜索特定的词(例如 "minutes")和这个的前一个词word(例如“10”)然后我想计算我所有分钟的总和。

我想这可能就是你想要的。

var text = 'Lorem ipsum dolor sit amet 10 minutes, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo 30 minutes consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est 60 minutes laborum.';

var re = new RegExp(/(\d+)\s+(?:minutes)/,'gm');

var sum = 0;
var val = 0;
while ((val = re.exec(text))) {
    sum += parseInt(val[1]);
}

console.log('sum', sum);