Google 文档的 Apps 脚本 findText()
Apps Script findText() for Google Docs
我正在将 RegEx 搜索应用于 Google Document 带有一些降价代码块标记 (```) 的文本。 运行 我文档中的以下代码返回空结果。
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 不支持我的代码中的某些正则表达式元素,但文档并未阐明这一点。有什么想法吗?
我也收到了 null - 我能够在段落中使用 3 ` 围绕单词 test 使下面的工作。
我确实找到了这个信息:
Apps 脚本中 class Text 对象的 findText 方法,扩展了 Google Docs。文档说“不完全支持 JavaScript 正则表达式功能的一个子集,例如捕获组和模式修饰符。”特别是,它不支持环视。
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);
}
}
我正在将 RegEx 搜索应用于 Google Document 带有一些降价代码块标记 (```) 的文本。 运行 我文档中的以下代码返回空结果。
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 不支持我的代码中的某些正则表达式元素,但文档并未阐明这一点。有什么想法吗?
我也收到了 null - 我能够在段落中使用 3 ` 围绕单词 test 使下面的工作。
我确实找到了这个信息: Apps 脚本中 class Text 对象的 findText 方法,扩展了 Google Docs。文档说“不完全支持 JavaScript 正则表达式功能的一个子集,例如捕获组和模式修饰符。”特别是,它不支持环视。
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);
}
}