Having issues with Google Apps Script replaceText

2019-09-06 16:00发布

问题:

So in my code

body.replaceText("test1","test2");

works and so do simple regexs, but I can't get more complex ones to work. For example I want to remove all spacing between two tags and replace it with three carriage returns. I've tested it out in regexr and other tools and it works but it does nothing in my Apps Script.

 body.replaceText("/</em>[\r\n]+<strong>/g","</em>\r\r\r<strong>");

this doesn't work either

body.replaceText("</em>[\r\n]+<strong>","</em>\r\r\r<strong>");

回答1:

i tried with this and it worked for me

body.replaceText("</em>\\[\\\\r\\\\n\\]\\+<strong>", '</em>\r\r\r<strong>');