Google Docs: insert newline in find-and-replace results
Asked Answered
G

3

16

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.

Galah answered 2/7, 2019 at 17:48 Comment(5)
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.Hazlett
@АлександрЕрмолин, are you suggesting a Google Docs programming feature, as opposed to typing things into the interactive find-and-replace command?Galah
Right. I mean the programming feature (non-interactive).Hazlett
Belongs on WebApps.StackExchange.comHumanly
Does this answer your question? find & replace commas with newline on Google SpreadsheetCheops
C
7
  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');
    }
Challenge answered 28/12, 2022 at 16:21 Comment(0)
S
1

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.

Sapro answered 9/10, 2022 at 17:53 Comment(2)
Unfortunately custom Apps Scripts don't work if you have Advanced Protection enabled on your Google account.Gonium
@Gonium As of today, Google Apps Script works fine with Google Advanced Protection enabled.Legato
T
0

When I drag my mouse over the last character in a line to the first character on the next line and open Find and Replace, it shows the two characters and an errored character between them that I can then copy. Whenever I paste this into the Find and Replace it works as you're intending for a newline. Example

Theotheobald answered 28/6 at 16:8 Comment(1)
Welcome to Stack Overflow. Instead of adding a link hosted elsewhere, use the insert image button on the editor toolbar.Legato

© 2022 - 2024 — McMap. All rights reserved.