适用于Google文档的Apps脚本findText() [英] Apps Script findText() for Google Docs
问题描述
我正在将RegEx搜索应用于 Google文档文本,一些降价代码块打勾(```).在我的文档上运行以下代码将返回空结果.
I'm applying RegEx search to a Google Document text with some markdown code block ticks (```). 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'
我怀疑我的代码中存在某些正则表达式元素,而 RE2不支持,但文档尚未对此进行说明.有什么想法吗?
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. Any ideas?
推荐答案
我也收到了null-我可以使用段落中单词test周围的3`来使下面的内容起作用.
I received null as well- I was able to get the below to work using 3 ` surrounding the word test within a paragraph.
我确实找到了以下信息: Apps脚本中Text类的对象的findText方法,扩展了Google Docs.文档说:不完全支持JavaScript正则表达式功能的子集,例如捕获组和模式修饰符."特别是,它不支持环视.
I did find this information: findText method of objects of class Text in Apps Script, extending Google Docs. Documentation says "A subset of the JavaScript regular expression features are not fully supported, such as capture groups and mode modifiers." 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);
}
}
这篇关于适用于Google文档的Apps脚本findText()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!