鉴于Google文档中的以下文字:

@1Bryan Smith
1234 Alameda Ln
Palm Springs, CO
92264@

@2Jerry Macnamara
4568 Montagonal Ave
Aspertine, MD
29382@

我希望能够以编程方式删除Bryan Smith或Jerry Macnamara的文本块 . 为此,我试图使用Apps Script body元素的replaceText()函数 . 问题是,正则表达式没有按预期运行 .

var i = 2;
var regex = new RegExp('^@' + i + '([\\s\\S]*)@$', 'm');

上面的正则表达式有效,但是body.replaceText()不将RegExp作为参数,只接受表示表达式的字符串 . 我试过用字符串格式复制上面的正则表达式:

body.replaceText('(m)^@' + i + '([\\s\\S]*)@$', '');

它无法找到匹配项 . 我相信我可能错误地设置了m标志 .

必须使用replaceText()函数,我不想直接将doc文本编辑为字符串,因为它会弄乱文档中的格式和图像 .

文件:https://developers.google.com/apps-script/reference/document/body#replacetextsearchpattern-replacement

用于测试的示例脚本(工具 - >脚本编辑器):https://docs.google.com/spreadsheets/d/136_o9acI_rtHf0njRapt1i-r0F_ZWPAXtKYVkUmpBlc/edit#gid=0