问题描述
我正在为Google文档编写一个应用程序脚本.我正在使用findText()来查找指定字符串的实例.
I am writing an apps script for Google Docs. I am using findText() to locate an instance of a specified string of characters.
默认情况下,它是区分大小写的,我需要删除它,但是我不知道如何将/i添加到re2正则表达式中,以便它可以在应用脚本引擎中工作.
By default, it is case sensitive and I need to remove that, but I can't figure out how to add the /i to the re2 regex so that it works in apps script engine.
在我的示例中,我尝试查找micssys的所有实例(例如micssys,Micssys,MICSSYS等).
In my example, I am trying for find all instances of micssys (e.g. micssys, Micssys, MICSSYS, etc).
现在我有:
var text = "micssys";
var bodyElement = DocumentApp.getActiveDocument().getBody();
var searchResult = bodyElement.findText(text);
我尝试过:
var searchResult = bodyElement.findText("/"+text+"/i");
var searchResult = bodyElement.findText(text+"/i");
var searchResult = bodyElement.findText(text+"(i)");
这些工作都没有.我想念什么
None of these work. What am I missing
推荐答案
如果我记得,我相信您可以创建一个新的regexp对象并在此处使用exec
.
If I recall, I believe you can create a new regexp object and use exec
here.
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()
以文本字符串的形式检索元素的内容,然后进行匹配.
Note: You may have to use getText()
to retrieve the contents of the element as a text string and then match against.
这篇关于Apps脚本正则表达式-不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!