简体   繁体   中英

Google Apps Script replaceText() not working with apostrophe " ' "

In my code, when I am trying to replace "don't" with "do not", it doesn't work, but when I use a regular word, it does. I have tried to escape with "don\\'t". Is there a special way to escape characters in google apps script?

//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";

}

Use replace() instead of replaceText()

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM