我試圖用findText
和正則表達式在一對兩個星號(即**This text, please**
)之間找到不確定數量的文本(包括標點符號)。然後該腳本應該對星號和隨附的文本採取行動。我的代碼如下:雙星號正則表達式報告爲無效
function main() {
var search = body.findText('(\*\*)+([.*]+)+(\*\*)');
if (search === null) {
DocumentApp.getUi().alert('No text was found that required formatting.');
} else {
function second() {
if (search !== null) {
search = body.findText('(\*\*)+([.*]+)+(\*\*)');
search.setBold(true);
body.replaceText('\*\*','');
second();
} else {
DocumentApp.getUi.alert('Formatting completed!');
}
};
}
};
我這樣的假設:工作findText
只找到一個搜索模式的第一個實例。 現在,我完全意識到我的正則表達式不起作用,因爲捕捉組不支持,但還有什麼其他方式來搜索「詞組」?如果不能用單個正則表達式完成,是否有可能通過三次單獨的搜索來完成此操作?
你.findText正則表達式下面不是雙星號之間的匹配是正確的。它應該更像:'\ * \ *(。*)\ * \ *'。 – MElliott
此外,.findText將返回true或false。你應該檢查真或假。不是「null」。 – MElliott
什麼是.replaceText()?這是來自其他插件或腳本的功能嗎?如何使用JavaScript'.replace()'?和search.setBold()?你正在調用.setBold()(不管那是什麼)布爾值..你的代碼有太多的問題.. – MElliott