Apps Script Regex - Case Insensitive

*爱你&永不变心* 提交于 2020-01-13 19:11:13

问题


I am writing an apps script for Google Docs. I am using findText() to locate an instance of a specified string of characters.

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.

In my example, I am trying for find all instances of micssys (e.g. micssys, Micssys, MICSSYS, etc).

Right now I have:

var text = "micssys";
var bodyElement = DocumentApp.getActiveDocument().getBody();
var searchResult = bodyElement.findText(text);

I have tried:

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


回答1:


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
}

Note: You may have to use getText() to retrieve the contents of the element as a text string and then match against.



来源:https://stackoverflow.com/questions/23900483/apps-script-regex-case-insensitive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!