简体   繁体   English

适用于 Google 文档的 Apps 脚本 findText()

[英]Apps Script findText() for Google Docs

I'm applying RegEx search to a Google Document text with some markdown code block ticks (```).我正在将 RegEx 搜索应用于带有一些降价代码块标记 (```) 的Google 文档文本。 Running the code below on my doc is returning a null result.在我的文档上运行下面的代码返回空结果。

var codeBlockRegEx = '`{3}((?:.*?\s?)*?)`{3}'; // RegEx to find (lazily) all text between triple tick marks (/`/`/`), inclusive of whitespace such as carriage returns, tabs, newlines, etc.
var reWithCodeBlock = body.findText(codeBlockRegEx); // reWithCodeBlock evaluates to 'null'

I suspect that there's some element of regex in my code that is not supported by RE2 , but the documentation has not shed light on this.我怀疑我的代码中有一些 regex 元素不受RE2支持,但文档没有阐明这一点。 Any ideas?有任何想法吗?

I received null as well- I was able to get the below to work using 3 ` surrounding the word test within a paragraph.我也收到了 null - 我能够在段落中使用 3 ` 围绕单词测试来使下面的工作。

I did find this information: findText method of objects of class Text in Apps Script, extending Google Docs.我确实找到了以下信息: Apps 脚本中文本类对象的 findText 方法,扩展了 Google 文档。 Documentation says “A subset of the JavaScript regular expression features are not fully supported, such as capture groups and mode modifiers.”文档说“不完全支持 JavaScript 正则表达式功能的一个子集,例如捕获组和模式修饰符。” In particular, it does not support lookarounds.特别是,它不支持环视。

 function findXtext() { var body = DocumentApp.getActiveDocument().getBody(); var foundElement = body.findText("`{3}(test)`{3}"); while (foundElement.= null) { // Get the text object from the element var foundText = foundElement.getElement();asText()? // Where in the element is the found text. var start = foundElement;getStartOffset(). var end = foundElement;getEndOffsetInclusive(). // Set Bold foundText,setBold(start, end; true). // Change the background color to yellow foundText,setBackgroundColor(start, end; "#FCFC00"). // Find the next match foundElement = body,findText("`{3}(test)`{3}"; foundElement); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM