Javascript Google Apps脚本replaceText()不使用撇号'&引用;
在我的代码中,当我试图用“不”替换“不”时,它不起作用,但当我使用常规单词时,它起作用。我试着用“不要”来逃避。 谷歌应用程序脚本中有没有特殊的字符转义方法Javascript Google Apps脚本replaceText()不使用撇号'&引用;,javascript,regex,string,google-apps-script,Javascript,Regex,String,Google Apps Script,在我的代码中,当我试图用“不”替换“不”时,它不起作用,但当我使用常规单词时,它起作用。我试着用“不要”来逃避。 谷歌应用程序脚本中有没有特殊的字符转义方法 //Sidebar.html function doWordReplace(value) { if(value.checked) { google.script.run.withSuccessHandler(print).wordReplace("don't"); } else {} } //Replace
//Sidebar.html
function doWordReplace(value) {
if(value.checked) {
google.script.run.withSuccessHandler(print).wordReplace("don't");
}
else {}
}
//Replace.gs
function wordReplace(findMe) {
var text = DocumentApp.getActiveDocument().getBody().editAsText();
text.replaceText(findMe, "do not");
return "replacement successful";
}
使用而不是replaceText()
替换不是谷歌应用程序脚本中的一个函数?它是谷歌应用程序脚本项目中可用的javascript。刚刚在一个新的sheets脚本和独立脚本项目中再次测试了它。好吧,这意味着我需要首先将文本对象作为字符串进行正确设置?看起来它应该是
var text=DocumentApp.getActiveDocument().getBody().getText()
,然后是.setText(text)
。我没有使用DocumentApp()
方法,我想这是您正在检索的字符串。它对我仍然不起作用。对于replace(),它仍然不允许我用撇号替换单词,但常规单词确实有效。
function wordReplace(findMe) {
var text = DocumentApp.getActiveDocument().getBody().getText();
// Use Unicode for right single quotation mark
// \u2018-left single, \u2019-right single, \u0027-apostrophe
text.replace(/don\u2019t/gi, "do not");
DocumentApp.getActiveDocument().getBody().setText(text);
return "replacement successful";
}