I'm trying to match certain text and then replace with the text plus some extra characters. Minimal example text:

#10 Oranges. These are citrus fruits

Desired output:

#10 Oranges. These are citrus fruits

Regex: (#\d{1,2}[^.]*\.)\s*

Replace with: $1\n

(I have Match using regular expressions checked)

The regex successfully matches #10 Oranges.. However the numbered backreference doesn't replace the text with the capture group but just goes in literally (literal output is $1\n). I have also tried using a backslash for the numbered backreference \1 and the result is the same.

Is there something I'm missing? How do I reinsert matched text in google docs using regular expressions?


4 Answers 4


Little hacky, but it doesn't require an extra add on script and will likely cover 99% of your use cases. You can still use capture groups with RegexReplace and reference in the replace text with $1 or $2. Just split up your regex into two capture groups and concatenate with a random (infrequently used) character like ~. Then you can take the entire returned value from that and replace ~ with a newline:



For more info on the regex syntax used in google sheets, see the re2 spec

  • 4
    the question is about Docs, not Sheets, as far as I understand. Commented May 6, 2019 at 9:04
  • 1
    I came here looking for a Sheets answer, and this is exactly what I was looking for! +1 from me.
    – Cullub
    Commented Oct 6, 2020 at 8:05

Short answer

Instead of using the built-in find and replace function use Google Apps Script or an add-on.


On the Find and Replace feature of Google Documents, the Replace part doesn't work with regular expressions and it doesn't work either with the replaceText() method from the Documents Service in Google Apps Script fortunately JavaScript replace method works.

To learn the very basics to create a simple script, see https://developers.google.com/apps-script/overview


This code is an adaptation of the one included in the reference

function myFunction() {
  var body = DocumentApp.getActiveDocument().getBody();
  var paragraphs = body.getParagraphs();
  for (var i=0; i<paragraphs.length; i++) {
    var text = paragraphs[i].getText();
       text.replace(/(\d{1,2}[^.]*\.)\s*/gi, '$1\n') );


  • 4
    I'll consider looking into this, but is the fact that replace doesn't work with regex a bug? According to this documentation, it looks like it should work: support.google.com/docs/answer/62754#regular_expressions (see the section Replace with regular expressions)
    – User
    Commented Nov 1, 2016 at 3:20

As stated by user in the comments

Note: Capture groups only work with Google Sheets.


As stated previously, you can't use regular expressions or capture groups in the replace field with Google Docs. But here's an easy workaround for a portion of your specific case.

To replace \n with a newline, just copy and paste an actual newline from your document to the replace field. It will show as a blank space in the field, but the newline is preserved and will replace all \n.

It's useful for example to replace double newlines \n\n or space before newline \n with only one newline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.