如何在 Google 文档文本编辑器中使用正则表达式查找和替换?
How do I find and replace using Regular Expressions in Google Docs Text Editor?
我正在尝试将我的笔记从 Kindle 导入 Google 文档(您可以查看它 here),并且我有一个文档,我想在其中删除所有出现以下文本(包括换行符):
Read more at location 6567 • Delete this highlight
Add a note
我想出了以下搜索模式并在 this google sheet 上对其进行了测试,以确保我的正则表达式语法有效:
"Read more at location (\d*) • Delete this highlight\nAdd a note"
然后我创建了一个 google 应用程序脚本,并将其加载到我的文档中:
function onOpen() {
DocumentApp.getUi() // Or DocumentApp or FormApp.
.createMenu('AdvancedFind&Replace')
.addItem('Remove Kindle HTML', 'findAndReplace')
.addToUi();
}
// In-Document Find and Replace
function findAndReplace() {
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText("Read more at location (\d*) • Delete this highlight\nAdd a note", "");
}
但是,当我 运行 它时,它不会替换文本。我认为这是 REGEX 的问题,因为当我改为 运行 这段代码时,它起作用了:
function replaceBat() {
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText("BBat", "BBAat REPLACEMENT SUCCESSFUL");
}
任何帮助将不胜感激,谢谢!
根据 documentation,某些模式可能不起作用:
A subset of the JavaScript regular expression features are not fully
supported, such as capture groups and mode modifiers.
见regex specifications in GoogleDocs here,并没有说支持\d
。所以,试试这个正则表达式:
^Read more at location [0-9]* • Delete this highlight[[:space:]]Add a note
或者
^Read more at location [0-9]*[^[:alpha:]]*Delete this highlight[[:space:]]Add a note
正则表达式不以引号开头和结尾;一个字符串。您的替换正在寻找文字字符串,而不是正则表达式。
尝试:
body.replaceText(/Read more at location (\d*) • Delete this highlight\nAdd a note/, "");
要获取所有实例,请添加 g
标志:
body.replaceText(/Read more at location (\d*) • Delete this highlight\nAdd a note/g, "");
问题是 Docs 正则表达式不支持“/d”匹配任何数字或“/s”匹配任何白色space字符,但支持 支持“[[:space:]]”匹配任意白色space字符!
以下语法适用于我的文档:
// In-Document Find and Replace
function findAndReplace() {
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText("^Read more at location [0-9]* • Delete this highlight[[:space:]]Add a note", "");
}
我在 https://www.google.com/support/enterprise/static/postini/docs/admin/en/admin_ee_cu/cm_regex.html
找到了 [[:space:]] 语法
看起来 Google 文档和表格现在允许您在搜索文本时以本机方式使用正则表达式。
只需打开本机搜索功能,您就会找到使用正则表达式的选项。
regex search functionality in google docs
您可以在此处找到更多信息:https://support.google.com/docs/answer/62754
我正在尝试将我的笔记从 Kindle 导入 Google 文档(您可以查看它 here),并且我有一个文档,我想在其中删除所有出现以下文本(包括换行符):
Read more at location 6567 • Delete this highlight
Add a note
我想出了以下搜索模式并在 this google sheet 上对其进行了测试,以确保我的正则表达式语法有效:
"Read more at location (\d*) • Delete this highlight\nAdd a note"
然后我创建了一个 google 应用程序脚本,并将其加载到我的文档中:
function onOpen() {
DocumentApp.getUi() // Or DocumentApp or FormApp.
.createMenu('AdvancedFind&Replace')
.addItem('Remove Kindle HTML', 'findAndReplace')
.addToUi();
}
// In-Document Find and Replace
function findAndReplace() {
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText("Read more at location (\d*) • Delete this highlight\nAdd a note", "");
}
但是,当我 运行 它时,它不会替换文本。我认为这是 REGEX 的问题,因为当我改为 运行 这段代码时,它起作用了:
function replaceBat() {
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText("BBat", "BBAat REPLACEMENT SUCCESSFUL");
}
任何帮助将不胜感激,谢谢!
根据 documentation,某些模式可能不起作用:
A subset of the JavaScript regular expression features are not fully supported, such as capture groups and mode modifiers.
见regex specifications in GoogleDocs here,并没有说支持\d
。所以,试试这个正则表达式:
^Read more at location [0-9]* • Delete this highlight[[:space:]]Add a note
或者
^Read more at location [0-9]*[^[:alpha:]]*Delete this highlight[[:space:]]Add a note
正则表达式不以引号开头和结尾;一个字符串。您的替换正在寻找文字字符串,而不是正则表达式。
尝试:
body.replaceText(/Read more at location (\d*) • Delete this highlight\nAdd a note/, "");
要获取所有实例,请添加 g
标志:
body.replaceText(/Read more at location (\d*) • Delete this highlight\nAdd a note/g, "");
问题是 Docs 正则表达式不支持“/d”匹配任何数字或“/s”匹配任何白色space字符,但支持 支持“[[:space:]]”匹配任意白色space字符!
以下语法适用于我的文档:
// In-Document Find and Replace
function findAndReplace() {
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText("^Read more at location [0-9]* • Delete this highlight[[:space:]]Add a note", "");
}
我在 https://www.google.com/support/enterprise/static/postini/docs/admin/en/admin_ee_cu/cm_regex.html
找到了 [[:space:]] 语法看起来 Google 文档和表格现在允许您在搜索文本时以本机方式使用正则表达式。
只需打开本机搜索功能,您就会找到使用正则表达式的选项。
regex search functionality in google docs
您可以在此处找到更多信息:https://support.google.com/docs/answer/62754