Google 文档:在查找和替换结果中插入换行符

Ste*_*eve 14 google-docs

我想改变这种形式的行:

one line find text
Run Code Online (Sandbox Code Playgroud)

插入一对这种形式的行:

first line of replace text
second line of replace text
Run Code Online (Sandbox Code Playgroud)

\n我知道可以通过选中“使用正则表达式匹配”框并填写“查找”框来搜索换行符。但\n在“替换为”框中似乎插入\n到了结果中,而不是所需的换行符。

我尝试使用此搜索和替换,但它对我不起作用:

Find          `one line of find text`

Replace with  `first line of replace text\nsecond line of replace text`
Run Code Online (Sandbox Code Playgroud)

我的下一个策略是在“查找”框中使用带括号的正则表达式和“$替换为”框中的 [number] 表达式的正则表达式匹配结构,但这对我来说也不起作用:

Find          `(\n)one line of find text`

Replace with  `$1first line of replace text$1second line of replace text`
Run Code Online (Sandbox Code Playgroud)

有没有办法在 Google 文档中执行此操作,或者我必须导出到功能更强大的文字处理程序(例如WordLibre Office),在那里进行查找和替换,然后导入回 Google 文档?

注 1:我描述的是截至 2019 年 7 月的 Google 文档行为。目前这可能是一个缺失的功能,但将来会成为一个功能性功能,这可能会在正确和错误之间翻转答案(除非它们包含“as的”限定词)。

注2:问题“在Google Docs上的正则表达式回车查找和替换”似乎解决了类似的问题,但它的措辞不够清楚,我无法判断它是否重复。一个答案提到了一种“HTML 模式”,但我在 Google 文档中没有看到这种模式。

Ale*_*idt 1

正如此处所建议的,可以通过转到“扩展”->“应用程序脚本”并添加如下内容来解决此问题:

var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
// remove extra spaces at the end of paragraphs.
body.replaceText('[ ]+$', '');
Run Code Online (Sandbox Code Playgroud)

然后单击Run并让此脚本在您的文档上运行。

请注意使用$代替\n,这是引用字符串末尾的正确方法。尽管这在使用文档中的“搜索和替换”内置功能时不起作用,但它适用于 Apps 脚本。