11

I want to change lines of this form:

one line find text

into this a pair of lines of this form:

first line of replace text
second line of replace text

I know that one can search for a newline by checking the "Match using regular expressions" box, and filling in \n in the "Find" box. But \n in the "Replace with" box appears to insert \n into the results, rather than the desired newline.

I tried using this search-and-replace, but it hasn't worked for me:

Find          `one line of find text`

Replace with  `first line of replace text\nsecond line of replace text`

My next strategy was to use the regular expression matching construct of a parenthesized regular expression in the Find box and a $[number] expression in the Replace with box, but it also didn't work for me:

Find          `(\n)one line of find text`

Replace with  `$1first line of replace text$1second line of replace text`

Is there a way to do this in Google Docs at all, or must I export to a more capable word processor (such as Word or Libre Office), do the find-and-replace there, and import back to Google Docs?

Note 1: I'm describing Google Docs behavior as of July 2019. It's possible that this is a missing feature for now, but will be a functional feature in the future, which could flip answers between right and wrong (unless they include "as of" qualifiers).

Note 2: The question "Regular Expression Carriage Return Find & Replace on Google Docs" appears to address a similar question, but it's not worded clearly enough for me to tell whether it's a duplicate. The one answer mentions an "HTML mode", but I don't see such a mode in Google Docs.

4
  • 2
    I am not agree with you, the method text.replaceText('one line of find text', 'first line of replace text\nsecond line of replace text'); works fine making all replacements at once. Jul 4, 2019 at 19:12
  • @АлександрЕрмолин, are you suggesting a Google Docs programming feature, as opposed to typing things into the interactive find-and-replace command?
    – Steve
    Aug 17, 2020 at 22:49
  • 1
    Right. I mean the programming feature (non-interactive). Aug 18, 2020 at 7:28
  • 1
    Belongs on WebApps.StackExchange.com
    – outis
    Sep 19, 2021 at 20:06

2 Answers 2

1
  1. In a Google doc, we can find-and-replace each instance of specified text with newline (or other special characters) using Google Apps Script.
  2. First, edit the doc.
  3. Select menu Extensions->Apps Script. That leads to an Apps Script Editor for a script that will apply only to that single document.
  4. For example, to insert a newline after every comma, edit/use the script below.
  5. Select Run.
    function myFunction() {
      var doc = DocumentApp.getActiveDocument(); 
      var body = doc.getBody(); 
      body.replaceText(',', ',\n');
    }
0

As suggested here, it is possible to solve this by going to Extensions -> Apps Script and adding something like this:

var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
// remove extra spaces at the end of paragraphs.
body.replaceText('[ ]+$', '');

Then click Run and have this script run over your document.

Note the use of $ instead of \n, which would be the right way to reference the end of the string. Even though this doesn't work when using the Search & Replace built-in function in Docs, it works for Apps Script.

1
  • Unfortunately custom Apps Scripts don't work if you have Advanced Protection enabled on your Google account.
    – Navin
    Nov 29, 2022 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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