regex - 将句子大写的谷歌文档脚本

标签 regex google-docs

我正在编写下面的 google doc 脚本以将文档中的句子大写。

function cap6() {

   var body = DocumentApp.getActiveDocument().getBody();
   var text = body.editAsText();

   var str1 = text.getText();
   Logger.log(str1);

   // define function "replacement" to change the matched pattern to uppercase
   function replacement(match) { return match.toUpperCase(); }

   // period followed by any number of blank spaces (1,2,3, etc.)
   var reg = /\.(\s*\s)[a-z]/g;

   // capitalize sentence
   var str2 = str1.replace(reg, replacement);
   Logger.log(str2);

   // replace string str1 by string str2
   text.replaceText(str1, str2);

}

从正确的结果显示在日志文件中的意义上来说,代码几乎可以正常工作,如下所示:

[15-10-22 22:37:03:562 EDT] capitalize sentences.  this is one example with ONE blank space after the period.  here is another example with TWO blank spaces after the period.          this is yet another example with MORE THAN THREE blank spaces.

[15-10-22 22:37:03:562 EDT] capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.

上面第一行是没有大写句子的原始段落;下面的第 2 行是带有大写句子的转换后的段落,无论句号后的空格有多少。

问题是我无法使用以下代码将 google 文档中的原始段落替换为转换后的段落:

   // replace string str1 by string str2
   text.replaceText(str1, str2);

我怀疑我在方法“replaceText”的参数中犯了错误。

如能帮助指出我的错误,我们将不胜感激。谢谢。

最佳答案

灵光一闪,我几乎用下面的代码解决了这个问题:

text.replaceText(".*", str2);

我的灵感实际上来自阅读有关 method "replaceText" 的文章.

当我在 google 文档中只有一个段落时,上面的代码有效。

但是当我在 google 文档中有两个段落时,代码给出了文档的副本,即原始两个段落正下方的两个段落的第二个精确副本(句子的大小写正确,包括第二段的开头,而不是第一段的开头)。

当我有3段时,我有这3段的3份,如下图:

capitalize sentences.  this is one example with ONE blank space after the period.  here is another example with TWO blank spaces after the period.          this is yet another example with MORE THAN THREE blank spaces.

capitalize sentences.  this is one example with ONE blank space after the period.  here is another example with TWO blank spaces after the period.          this is yet another example with MORE THAN THREE blank spaces.

capitalize sentences.  this is one example with ONE blank space after the period.  here is another example with TWO blank spaces after the period.          this is yet another example with MORE THAN THREE blank spaces.

然后运行脚本后,我得到了这3段的3份(句子大小写正确,包括第2段和第3段的开头),如下图:

capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.

Capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.

Capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.







capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.

Capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.

Capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.







capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.

Capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.

Capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.

所以新代码中仍然存在一些问题...如果我可以去掉文档的额外副本,它几乎可以工作。

回到原来的代码

text.replaceText(str1, str2);

我怀疑在方法“replaceText”的第一个参数中使用变量“str1”有问题。希望有高手能解释一下我原来代码中的错误。

关于regex - 将句子大写的谷歌文档脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33294140/

相关文章:

python - 整数和整数的正则表达式?

ruby-on-rails - 如何将整个单词与 Aho corasick 匹配?

python - Unicode、正则表达式和 PyPy

python - django/python : Google doc viewer issue with . docx/.xlsx/.pptx

google-apps-script - Google 脚本 - 在 Google 文档表中插入图像

google-apps-script - Google 电子表格脚本 - 如何测试单元格是否为粗体

c# - 正则表达式替换无效字符

regex - 非贪婪字符串正则表达式匹配

java - 如何在 Android 中使用 Google 表单

google-apps-script - 以编程方式将容器绑定(bind)触发器添加到文档