Google Apps 脚本 - replaceText - 无法替换句点、逗号和问号
Google Apps Script - replaceText - Can't Replace Period, Comma and Question Mark
我正在尝试编写一个 google 应用程序脚本,它将在 google 文档中查找并用其他词替换特定词...
我想将“你好”(space, 你好, space) 替换为“R1”(space, R1, space)
如果你好后面有任何标点符号,如句号、逗号或问号,它应该是相同的逻辑:
" hello "
将替换为 " R1 "
" hello. "
将替换为 " R1. "
" hello, "
将替换为 " R1, "
" hello? "
将替换为 " R1? "
所以我使用了以下内容:
function docReplace() {
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText(" hello ", " R1 ");
body.replaceText(" hello. ", " R1. ");
body.replaceText(" hello, ", " R1, ");
body.replaceText(" hello? ", " R1? ");
}
不幸的是,这不起作用,因为“。” , “,“ 和 ”?”是正则表达式符号。
然后,我尝试了这个:
function docReplace() {
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText(" hello ", " R1 ");
body.replaceText(" hello\. ", " R1. ");
body.replaceText(" hello\, ", " R1, ");
body.replaceText(" hello\? ", " R1? ");
}
但还是不行。逗号和问号 return 作为句点。
如果有人能帮助我提供正确的代码,我将不胜感激。
您想使用 Google Apps 脚本实现以下替换。在此示例中,##
用作值的分隔符。
来自
## hello ##
## hello. ##
## hello, ##
## hello? ##
至
## R1 ##
## R1. ##
## R1, ##
## R1? ##
如果我的理解是正确的,这个修改怎么样?本次修改中,\.
、\,
、\?
分别修改为\.
、\,
、\?
。
修改后的脚本:
function docReplace() {
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText(" hello ", " R1 "); // Modified
body.replaceText(" hello\. ", " R1. "); // Modified
body.replaceText(" hello\, ", " R1, "); // Modified
body.replaceText(" hello\? ", " R1? "); // Modified
}
参考:
如果我误解了您的问题而这不是您想要的结果,我深表歉意。
我正在尝试编写一个 google 应用程序脚本,它将在 google 文档中查找并用其他词替换特定词...
我想将“你好”(space, 你好, space) 替换为“R1”(space, R1, space) 如果你好后面有任何标点符号,如句号、逗号或问号,它应该是相同的逻辑:
" hello "
将替换为" R1 "
" hello. "
将替换为" R1. "
" hello, "
将替换为" R1, "
" hello? "
将替换为" R1? "
所以我使用了以下内容:
function docReplace() {
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText(" hello ", " R1 ");
body.replaceText(" hello. ", " R1. ");
body.replaceText(" hello, ", " R1, ");
body.replaceText(" hello? ", " R1? ");
}
不幸的是,这不起作用,因为“。” , “,“ 和 ”?”是正则表达式符号。
然后,我尝试了这个:
function docReplace() {
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText(" hello ", " R1 ");
body.replaceText(" hello\. ", " R1. ");
body.replaceText(" hello\, ", " R1, ");
body.replaceText(" hello\? ", " R1? ");
}
但还是不行。逗号和问号 return 作为句点。
如果有人能帮助我提供正确的代码,我将不胜感激。
您想使用 Google Apps 脚本实现以下替换。在此示例中,
##
用作值的分隔符。来自
## hello ## ## hello. ## ## hello, ## ## hello? ##
至
## R1 ## ## R1. ## ## R1, ## ## R1? ##
如果我的理解是正确的,这个修改怎么样?本次修改中,\.
、\,
、\?
分别修改为\.
、\,
、\?
。
修改后的脚本:
function docReplace() {
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText(" hello ", " R1 "); // Modified
body.replaceText(" hello\. ", " R1. "); // Modified
body.replaceText(" hello\, ", " R1, "); // Modified
body.replaceText(" hello\? ", " R1? "); // Modified
}
参考:
如果我误解了您的问题而这不是您想要的结果,我深表歉意。