Regular expressions have a reputation for being a programmer’s tool, but the core of them is just a smarter find-and-replace — one that can match patterns rather than exact text. For a writer or editor cleaning up a manuscript, that is enormously useful: you can fix every doubled space, every mis-spaced dash, or every instance of a formatting quirk in one pass. You do not need to program. You need about a dozen symbols, which this guide covers with worked examples.
The idea: matching patterns, not exact words
Ordinary find-and-replace looks for literal text: you type “colour” and it finds “colour”. A regular expression describes a shape. Instead of “find this exact string”, you say “find any digit”, or “find two or more spaces in a row”, or “find a word at the start of a line”. Anything that follows a rule — even a loose one — can be matched, which is why regex handles the messy, repetitive edits that literal search cannot.
Because a small mistake in a pattern can match far more than you intended, always test before you replace. Seeing which parts of your text light up as matches, before you commit, is the difference between a clean edit and a mangled document.
Regex TesterBuild a pattern and watch every match highlight in your own text before you use it to replace anything.The dozen symbols worth knowing
This handful covers most editing tasks. Everything else is a variation on these.
- \d matches any single digit; \D matches any non-digit.
- \w matches a word character (letter, digit, or underscore); \W matches the opposite.
- \s matches any whitespace — a space, tab, or line break; \S matches a non-space.
- A full stop matches any single character at all.
- + means “one or more of the thing before it”; * means “zero or more”.
- ? means “optional — zero or one”.
- ^ anchors to the start of a line; $ anchors to the end.
- Square brackets match any one character from a set, so [aeiou] matches any vowel.
- Parentheses group part of a pattern and capture it for reuse in the replacement.
- A backslash escapes a special character, so \. matches a literal full stop rather than any character.
Worked examples for real editing
The point of the symbols is what you can build from them. Here are patterns that solve problems that come up in almost every manuscript.
- Collapse doubled spaces: search for two or more spaces (a space followed by +) and replace with a single space.
- Remove trailing spaces at the end of lines: search for one or more spaces followed by $ and replace with nothing.
- Add a space after a comma that is missing one: find ,(\S) and replace with a comma, a space, and the captured group.
- Find a straight apostrophe between letters: \w'\w, to catch contractions you may want to convert.
- Match a full date like 12/09/2026: \d+/\d+/\d+.
- Find any word doubled by mistake, such as “the the”: (\w+) \1, where \1 refers back to the first captured group.
That last example shows the real power move: capturing part of a match with parentheses and referring to it again. The pattern (\w+) \1 means “a word, a space, then the same word again” — which finds the classic repeated-word typo that spellcheckers miss.
Capture groups in replacements
Groups are just as useful on the replacement side. Suppose you have a list of names as “Surname, Firstname” and want “Firstname Surname”. Match (\w+), (\w+) and replace with the second group, a space, then the first group — written as $2 $1 in most tools. The two names swap in a single pass, however long the list.
This is where regex earns its keep. Reformatting a hundred entries by hand is tedious and error-prone; a tested pattern does it uniformly in one action. The key word is tested — a group referenced wrongly will scramble the output, so confirm the result on a few lines first.
Find & ReplaceTurn on the regex option to run a tested pattern with capture groups across the whole document, with every match highlighted first.Common traps to avoid
Two things catch out newcomers more than anything else. The first is greedy matching: by default, + and * grab as much as they can. A pattern meant to match one HTML tag will, on a line with two tags, swallow everything from the first opening bracket to the last closing one. Adding a ? after the + makes it lazy and stop at the first match.
The second is forgetting to escape special characters. A full stop in a pattern means “any character”, so to match an actual full stop you write \. — the same goes for ?, +, *, and the bracket characters. If a pattern matches far more than you expected, an unescaped special character is the usual reason.
- Always test a pattern on a copy or in a tester before replacing across a whole document.
- Start specific and loosen only if you need to — a broad pattern matches things you did not intend.
- Watch for greedy quantifiers swallowing more than one match on a line.
- Escape the special characters . ? + * ( ) [ ] $ when you mean them literally.
Everything here runs in your browser. Testing a pattern against a chapter of a novel or a confidential document does not upload that text anywhere — the matching happens locally, so you can experiment freely with sensitive material and keep only the cleaned result.