Using RegEx in Google Docs

We all know about Ctrl+F for Find, and most of us know about Ctrl+H for find and replace (shown below). The “Match case” box is handy for finding things that exactly match the upper-case or lower-case letters you type. But what’s that box labeled “Match using regular expressions” for?

Regular Expressions, or RegEx for short, are tools for searching through text. They allow us to perform more complex searches.

We’ll work through several examples, from simple to complex, to show how useful RegEx can be.

Example 1

For example, let’s say you want to search for more than one word at once. You’d have to run a bunch of searches. But instead, you can do one using a Regular Expression: (word1|word2|word3) and so on. For example:

Example 2

Note that in the example above, the search accidentally picks up the “he” in the word “the”. How do we search for “whole words only”?

One of the most useful RegEx tokens to know is \b which means a word boundary. This allows you to search for “whole words only”. Let’s take Example 1 above. If you change it to \b(he|him|his)\b you won’t accidentally pick up the “he” part of the word “the” (and if you wanted to find only matches within other words, you’d use two \B instead!)

Let’s use \b in conjunction with [A-Z] to find words that are written in ALL CAPS. We can do this with the RegEx string \b[A-Z]{2,}\b

  • \b indicates a word boundry (the spaces around a word)
  • [A-Z] says to look for these alphabet letters
  • {2,} says to look for the letters A-Z used 2 or more times (if it was {1,} it would pick up the word “I”). (You could also use {2,8} to search for words that are at least 2 but no more than 8 letters long, for example.)

The example above will find ALL the all-caps words, not just LASER. If you want to replace specific words (lets say you inconsistently used LASER and laser), you’d do it like this: \b(LASER)\b

Example 3

Let’s say you have a bad habit of occasionally using the antiquated “said Bill” instead of the more modern “Bill said” and you want to go through your text and find them all.

You can search for that with said [A-Z] which says to look for the word said, followed by any the letters A-Z. Note that if you turn on “Match case” it will only search for capital letters.

Well, that caught three of the four problems. Let’s try to modify that search to make it more universal. We can add some other dialogue verbs to the list: (said|yelled|muttered) [A-Z]

Now we’ve found them all.

Example 4

Let’s say you are a bit inconsistent in how you use okay (sometimes writing OK, ok, O.K., o.k., or okay), and you want to replace them all with just “okay”. Well, you don’t want to replace the “ok” in the middle of the word “broken”, or any other words.

So, we construct a RegEx pattern like so: \b(ok)\b to find whole words only

(You don’t technically need the parentheses here, but it’s easier to read with them.)

Well, that found one of the “ok” uses, and it didn’t find the “ok” in the middle of “smokes”, so that’s good. But how do we find the others?

Unfortunately, \b(ok|o.k.)\b doesn’t work, because the period (.) is a special character in RegEx. We can “escape” the period and search for it literally with a backslash, like so: \b(ok|o\.k\.)\b but this doesn’t work either because \b is a word boundary, and word boundaries include periods.

Instead we can use \W(ok|o\.k\.)\W because the token \W searches for any non-word character:

Example 5

The above highlights both versions of OK, and it works if you’re trying to find something. But it isn’t ideal if you also want to use “Replace”, because it includes these excess characters, which will vary and can’t be easily be replaced.

To fix this, we need to use lookbehinds and lookaheads.

  • A lookbehind looks for things before (behind) some text. It looks like this: (?<=[stufftosearch]|^)
  • A lookahead looks for things after (ahead of) some text. It looks like this: (?=[stufftosearch]|$)

Note that the ^ means to also look at the start of a line, and the $ means to also look at the end of a line. Typically this is actually the start or end of the entire document. You don’t technically need these, but it’s a good idea when using lookbehinds/lookaheads.

Since we are searching for non-word characters with \W, let’s try that. We put the (ok|o.k.) in between, like so: (?<=\W|^)(ok|o.k.)(?=\W|$)

That works! If we replace that (ok|o\.k\.) with the capitalized version (OK|O\.K\.) we can find those too.

Example 6

Now that you know what lookaheads and lookbehinds are, here’s another very handy use of them: replacing simple quotation marks with pretty quotation marks.

  • Simple quotes are " and '
  • Pretty quotes are “ ” and ‘ ’

So, we can use these RegEx searches (\w is a “word character”, meaning those characters you would use in a word):

  • \"(?=\w) searches for simple double quotes at the beginning of a word, so you can replace them with
    • Explanation: Searches for a double quotation mark, followed by a word character (\w)
  • (?<=\S)\" searches for simple double quotes at the end of a word, so you can replace them with
    • Explanation: Searches for a non-whitespace character (\S), which will include question marks, em dashes etc, followed by a double-quotation mark
  • \B\'(?=\w) searches for simple single quotes at the beginning of a word, so you can replace them with
    • Explanation: Searches for a non-word boundary (\B), followed by a single quote, followed by a word-character. We need to \B to avoid accidentally picking up apostrophes in contractions, like can’t.
  • (?<=\S)\' searches for simple single quotes in the middle or at the end of a word, so you can replace them with
    • Explanation: Searches for a non-whitespace character (\S), which will include question marks, em dashes etc, followed by a double-quotation mark

Here’s a screenshot of the last one:

You can also replace pretty quotes with simple quotes, but it’s much easier RegEx since simple quotes are not directional.

  • Search for (“|”) and replace with "
  • Search for (|) and replace with '

Example 7

Let’s get back to Example 5 and the issues with O.K. How do you detect “O.K.” at the beginning of a sentence, versus “O.K.” used in the middle of the sentence?

Let’s use this passage:

“OK,” Bill said. “I’ll take him outside and kill him over a grape.”
“Great,” the clerk said, “make sure to clean up after, OK?”
Bill pulled a gun. OK, it was a water pistol.
“Wait,” the thief said, “I want to live.”
Bill put away the gun. “OK, then pay the man for that grape you ate.”
“OK, OK,” the thief said. “I’m sorry, OK. Where are you from?”
“Oklahoma,” Bill said, “OK?”

Some of these OKs needs to be replaced with “Okay” and the others with “okay”. How do we know?

You can use lookbehinds and lookaheads to figure it out.

The final solution for the capitalized ones is as follows:
(?<=([.!?\s]\s|[.!?\s]\s”|[\n\r]”|^”|^))(ok|o.k.)(?=(\W|$))

Let’s break that up:

  • (?<=( beginning of lookbehind
  • [.!?\s]\s searches for any [ending punctuation, space characters (\s)] followed by a space (second \s). This will match cases at the beginning of a sentence, like:
    Bill pulled a gun. OK, it was a water pistol.
  • |acts as an “or” operator
  • [.!?\s]\s” searches for the same thing, in dialogue format.
    Bill put away the gun. “OK, then pay the man for that grape you ate.”
  • |acts as an “or” operator
  • [\n\r]” searches for a new line (\n) or carriage return (\r) followed by a quotation mark, for dialogue lines that start with OK, like:
    “OK, OK,” the thief said. “I’m sorry, OK.”
  • |acts as an “or” operator
  • ^” searches at the very beginning of the entire document, like:
    “OK,” Bill said. “I’ll take him outside and kill him over a grape.”
    • Unfortunately, Google Docs doesn’t support this properly (for shame) but it is correct RegEx.
  • |acts as an “or” operator
  • ^ searches at the very beginning of the entire document, but without a quote after it, in case you literally started the file with something like:
    OK, this is the story of the stolen grape.
    • Unfortunately, Google Docs doesn’t support this properly (for shame) but it is correct RegEx.
  • )) closes the lookbehind
  • (OK|O.K.) is what we’re searching for (either OK or O.K.)
  • (?=(\W|$)) is the lookahead, which is what we want after the OK – a whitespace character (\W) so we don’t accidentally pick up things like
    “Oklahoma is where I’m from.” and accidentally turn it into
    “Okaylahoma is where I’m from.”

Note that if you’re using pretty quotes ( “ instead of " ) you may need to search for those instead.

Here it is in text (unfortunately due to the limitations of Google Docs, the first one isn’t caught, but how many documents start with “OK” as their first word?):

Example 8

And then here is the much-simpler lower-case version of Example 7:
(?<=([\w,] |, “))(OK|O.K.)(?=(\W|$))

  • (?<=( beginning of lookbehind
  • [\w,] searches for either a word character (\w) or a comma, followed by a space (literal space this time, not \s). This will match cases in the middle of a sentence, like:
    I’m sorry, OK.
  • |acts as an “or” operator
  • , “ searches for the same thing, in dialogue format, a comma followed by a space followed by a quotation mark.
    Bill put away the gun. “OK, then pay the man for that grape you ate.”
  • )) closes the lookbehind
  • (OK|O.K.) is what we’re searching for (either OK or O.K.)
  • (?=(\W|$)) is the lookahead, which is what we want after the OK – a whitespace character (\W) so we don’t accidentally pick up things like
    “I’m from Oklahoma.” and accidentally turn it into
    “I’m from Okaylahoma.”

Note that if you’re using pretty quotes ( “ instead of ” ) you may need to search for those instead.

Here it is in text:

If you used say, a ton of variants of okay, you could replace the middle with something like: (ok|OK|o\.k\.|O\.K\.) to catch them all.

Closing thoughts

When you’re building your RegEx patterns, I strongly recommend using RegEx101.com which is a great tool. Just make sure to set the “flavor” on the left to ECMA Script (Javascript), which is what Google Docs uses. There is a great glossary of all the RegEx special characters and terms in the lower right panel as well.

Also note that certain types of operations, such as Negative Lookbehinds, are not supported by all browsers. Chrome has the best support for the latest RegEx tools.

Good luck, and I hope you now understand a bit more about RegEx and also why you should just use “okay” instead of the abbreviations!

Distraction-free writing in Google Docs

Writing without distractions in Google Docs is easy! Here are the steps.

Press F11 on your keyboard to go to full-screen mode in your browser. This will hide most of the browser menu bars and your operating system menus. You can press F11 again to turn it off.

Optional: Increase zoom size. Depending on the size of your screen, you may want to increase the zoom. I usually use 150% or 200% for full-screen writing. You can also choose “Fit” at the top to make the text as wide as will fit the screen!

Turn off all the optional view items, if they are on.

Go to the View Menu and choose “Full screen”

Now you are writing distraction-free!

Optional: Invert the colors

If you find that the bright white background is hard on your eyes, you can invert the colors by installing an Add-on / Extension in your browser to invert the colors. (You can also change the text color and the page color, but it doesn’t affect the entire screen.)

I’ve found that the Chrome Add-on “Dark Background and Light Text” works very well with Google Docs when set to “Invert” mode. Here is the link to the same extension for Firefox. It’s an open-source extension for your browser. Using it and the tips above, my full screen looks like this:

It may seem too dark in the screenshot, but it’s actually just about perfect for me, and I find it far less tiring on the eyes than black text on a white screen.

The extension will give you a little icon  A  in your Chrome menu bar that you can click on change the brightness settings. If you right-click on it and choose “Options” you can set up your default setting (e.g. leave it disabled unless you manually turn it on, or leave it on unless you manually disable it) as well as the colors for the stylesheet processor mode.

Optional: Distraction Free Mode browser extension

Alternatively, you can install “Distraction Free Mode“, an Add-on / Extension for your browser (Chrome, Firefox, or Safari) that does this all in one click. It works well, but it lacks some of the customization options that the above method provides.

Now that you know these tips, you can easily set up your preferred distraction-free writing environment in Google Docs!

Optional: Set up offline editing, and then disconnect from the internet

To truly go distraction free, unplug from the internet! If you use Chrome as a browser, you can set up Google Docs Offline Mode to save your edits locally and then upload them to your Drive when you reconnect. I have used this extensively while flying (airplane mode) and it works great.

Hugo, Nebula, World Fantasy Awards Combined Novel List

In the Google Docs link below, you’ll find a combined list, without duplicates, of all the winners and nominees of the Hugo, Nebula, and World Fantasy awards for Best Novel. The novels are sorted by author’s last name (to aid in book shop hunts).

Feel free to save your own copy using “File -> Make a copy”. With your own copy, you can use the green filter icons at the top of each column to sort or filter the table by that column. For example, if you only wanted to see winners, and not nominees, you would filter by the first “won/nom” column for only “won”.

Google Docs Spreadsheet link: https://docs.google.com/spreadsheets/d/1GJAWbhrUa9qEM16YZEZNMnEJpQDuwreOqCZgAwHjAns/edit?usp=sharing

If you are interested in chatting about novel writing, check out our Discord Server, The Library, a chatroom for fiction writers. Discord is a free and easy-to-use chat application for the web, desktop, and mobile devices.

Snowflake Method Spreadsheet for Google Docs

What is the Snowflake Method?  It’s a way to outline your novel, starting with a single sentence, and then building that into a paragraph, then several paragraphs, then pages, and then a full story.*

The Snowflake Method was popularized by Randy Ingermanson and you can read more about it on his website.  He also publishes software that lets you do a much deeper dive into the method than this simple spreadsheet.

Here is the link to the Google Spreadsheet:
https://docs.google.com/spreadsheets/d/1MQjikpzlNqADSwS0LUOGNM4D3imu1gPAck1q2kW2gFo/edit?usp=sharing

If you want to walk through the entire Snowflake Method, then you can start at the first tab and work your way to the right.  If you like to keep track of your scenes and characters in a spreadsheet, you may want to copy only those two sheets out of this file, as they are the most useful once you get your basic plot outlined.

Once you open the spreadsheet, you can go to “File” -> “Make a copy” to save a copy to your own Google Drive.

Good luck!

*  Really, it should be called the snowball method, as that is a much more accessible metaphor than something about fractals.

Google Docs Add-ons for Writers

Here are some useful Add-ons that you can use within Google Docs™.

To install an Add-on, go to the Add-ons menu and click “Get Add-ons”.

Writer’s Highlighter

This useful plugin allows you to highlight words from a word list in a Google Spreadsheet™.  You can highlight from a word list, highlight a selection, highlight continuously as you type, and highlight sentences by length. It also has functions to clear highlights from the entire doc or a selection.  It also includes a stats viewer for word count, word frequency, lists of all the sentences, and readability statistics.  The help file is here.

 

OneLook Thesaurus

While you can get some synonyms from Google’s thesaurus by selecting a word, right-clicking it, and choosing “Define”, it’s not comprehensive.

OneLook Thesaurus provides standard synonyms, rhymes, and frequently used words that often appear near the word you are looking up.

  

Word Cloud Generator

This plugin generates a word cloud from your source material.  This can be very useful to understand the frequency of occurrence of the words in your document, or just to share word clouds with friends.  The word clouds are generated as transparent-background .png files, you can can copy-and-paste them into any website or social app.

  

Better Word Count

This simple tool allows you to get a word and character count, but exclude certain sections of the document, such as headers or titles.

  

Google Translate

First written as a demo app by Google to highlight the extensibility of Google Docs™, this app has useful tools for quick and easy translations.

  

Screenplay Formatter

For some reason that has been lost to time, screenplays are still put in an archaic manuscript format, despite the fact that no one has written a screenplay on a typewriter in twenty years.  However, if you are writing a screenplay, you must play by the rules.  This add-on will help you do that.