Different regex behavior between Google Sheets and Google Docs replacement functions

I’m having trouble understanding why regex patterns work differently when used in Google Sheets versus Google Docs.

In my Google Docs document, I have this text:

London on ${timestamp}

When I use this formula in Google Sheets:

=REGEXREPLACE(A1,"\$\{timestamp\}","15/03/21")

It works perfectly and replaces the placeholder as expected.

However, when I try the same pattern in Google Apps Script for Docs:

function updateDocument(){
  var currentDoc = DocumentApp.getActiveDocument();
  var documentBody = currentDoc.getBody();
  documentBody.replaceText("\$\{timestamp\}","15/03/21");
  // this doesn't work
}

The ${timestamp} placeholder doesn’t get replaced at all. When I test with simpler text like “London”, the replacement works fine.

I’m not looking for alternative solutions since I’ve found workarounds. I just want to understand what I’m doing wrong with the regex pattern in Google Docs.

Had the exact same frustration building a document automation system a few months ago. Here’s what’s happening: JavaScript treats your string as a literal first, so those backslashes get eaten during string parsing. By the time your pattern hits replaceText, it’s already stripped down. Sheets skips this string literal processing step entirely in REGEXREPLACE. What saved me was logging the pattern to console first to see what actually got passed to the regex engine. You’ll probably find it’s not what you expect. The double escaping approach others mentioned works, but understanding this string processing difference will save you headaches with future patterns.

The issue arises from the way Google Apps Script interprets strings compared to Google Sheets. In JavaScript, backslashes are treated as escape characters. This means that when you write "\$\{timestamp\}", it gets processed as \$\{timestamp\} before reaching the regex engine. I’ve encountered this problem while automating documents, and the solution is to double-escape the backslashes: "\\\$\\\{timestamp\\\}". Although this may seem complex, it ensures the regex receives the correct pattern after JavaScript processes the string. On the other hand, Sheets handles this more straightforwardly, which is why you’re not facing the same issue in that environment.

Google Apps Script and Sheets use different regex engines, so you’ve got to escape backslashes differently in each one.

These regex inconsistencies across Google services are a headache. I wasted a lot of time debugging this exact problem when automating document workflows.

I now use Latenode for everything. It allows me to set up workflows that process documents with consistent regex patterns, no matter if I’m using Sheets, Docs, or anything else. No more guessing which escaping method actually works.

I created a template replacement system that handles these edge cases automatically. Works every time without dealing with platform-specific quirks.

docs are kinda picky with regex, right? try using \\$\\{timestamp\\} in your script. extra backslashes might do the trick since they use RE2 while sheets don’t.