我正在为Google Docs编写一个应用程序脚本。我正在使用findText()来查找指定字符串的实例。
默认情况下,它是大小写敏感的,我需要删除它,但我不知道如何将/I添加到re2正则表达式中,以便它在应用程序脚本引擎中工作。
在我的例子中,我试图找到micssys的所有实例(例如micssys, micssys, micssys等)。
现在我有:
var text = "micssys";
var bodyElement = DocumentApp.getActiveDocument().getBody();
var searchResult = bodyElement.findText(text);
I have try:
var searchResult = bodyElement.findText("/"+text+"/i");
var searchResult = bodyElement.findText(text+"/i");
var searchResult = bodyElement.findText(text+"(i)");
这些都不起作用。我错过了什么
如果我没有记错的话,我相信您可以在这里创建一个新的regexp对象并使用exec
。
var re = new RegExp('\bmicssys\b','gi');
var match;
var bodyElement = DocumentApp.getActiveDocument().getBody();
while (match = re.exec(bodyElement)) {
// match[0] will return the found results
}
注意:您可能必须使用getText()
来检索元素的内容作为文本字符串,然后匹配。