Skip to content

Find and Replace With Regex: A Cookbook for Writers and Editors

Regex find and replace in one paragraph

A plain find box changes one exact string. A regular expression (regex) changes every piece of text that fits a pattern: any run of two or more spaces, every date written as 2026-11-04, every word typed twice in a row. This guide is a cookbook of patterns that writers and editors actually need, each with an input and the exact output it produces.

Every example below was run through the code of WritePadPro's Find and Replace Text tool, so the match counts and results are what you will see when you paste the same text. Nothing is uploaded: the search runs in your browser.

How the tool reads your pattern

Patterns use JavaScript (ECMAScript) regular expression syntax, the same flavor browsers use. A few settings are fixed, and knowing them saves a lot of head-scratching:

  • Multiline is always on. ^ and $ match at the start and end of every line, not only the start and end of the whole text.
  • Matching ignores case unless you tick Match case. That applies to letters in every alphabet, so É matches é.
  • The dot does not match line breaks. Use [\s\S] when you mean "any character at all".
  • Unicode property escapes work. \p{L} means any letter in any script and \p{Lu} any capital letter. The tool switches on JavaScript's Unicode mode for you when your pattern contains them.
  • Rules run from top to bottom. Each rule works on the text the rules above it produced, and you can switch a rule off, move it, or delete it.

In the Replace box of a regex rule you can use $1 to $99 for numbered groups, $<name> for a named group, $& for the whole match and $$ for a dollar sign. \n inserts a line break, \t a tab, and \1 to \9 are accepted as aliases of $1 to $9. Plain-text rules insert the replacement exactly as typed.

For the full syntax, MDN's guide to JavaScript regular expressions is the reference we checked this article against on 2026-10-14.

Test safely before you replace anything

Regex is powerful because one rule can change thousands of places at once. That is also the risk. A safe routine:

  1. Read the match count first. Each rule shows how many matches it found as you type. If you expected 12 and see 412, the pattern is too broad.
  2. Check the preview. "Show changes" strikes through every deletion in red and underlines every insertion in green, word by word. Scan it before you press Replace All.
  3. Build one rule at a time. Add the next rule only when the current one does exactly what you want.
  4. Keep an exit. Undo and Redo step through every Replace All. For long documents, keep the original in a second tab, or compare before and after in the Text Diff Checker.

Two safety nets are built in. Patterns that can take exponential time, such as a group that repeats a group that already repeats, are refused before the search starts. Every regex search also runs in a background worker that is stopped after three seconds, so a slow pattern cannot freeze the page.

The cookbook: spacing and punctuation

Collapse double spaces

Find {2,} (a space followed by {2,}), replace with a single space.

Before and after (3 matches):

This  sentence has   extra spaces.  So does this one.
This sentence has extra spaces. So does this one.

Why not just replace two spaces with one in a plain rule? Because a plain rule makes one pass: four spaces become two, not one. The {2,} quantifier catches any run in a single pass. For non-breaking spaces and other unusual spacing, the Remove Extra Spaces tool handles more cases, and our guide to fixing text spacing explains them.

Remove spaces at the end of lines

Find [ \t]+$, replace with nothing. Because $ matches at every line end, this cleans each line of a pasted list or code block.

Three lines where the first ends in three spaces and the second in a tab give 2 matches, and both lines lose their invisible tail.

Remove the space before punctuation

Find +([,.;:!?]), replace with $1. The brackets capture the punctuation mark so the replacement can put it back without the space.

  • Before: Wait , what ? Really !
  • After: Wait, what? Really! (3 matches)

Leave this rule off for French text, where a space before ; : ! and ? is standard typography.

Collapse runs of blank lines

Find \n{3,}, replace with \n\n. Three or more line breaks in a row (two or more empty lines) become one empty line between paragraphs. A line that contains only spaces is not empty to this pattern, so run the end-of-line rule above first.

Straight quotes to curly quotes

This takes five rules, and the order matters:

RuleFindReplaceWhat it handles
1'(?=\d0s\b)’Decades such as '90s (an apostrophe, not an opening quote)
2(?<=^|[\s(\[{—–-])"“A double quote at a line start or after a space, bracket or dash opens
3"”Every remaining double quote closes
4(?<=^|[\s(\[{—–-])'‘Opening single quotes
5'’Every remaining single quote is a closing quote or an apostrophe

Before:

Back in the '90s, she said, "It's Sam's 'blue' book."
"Yes," he said.

After:

Back in the ’90s, she said, “It’s Sam’s ‘blue’ book.”
“Yes,” he said.

The rules found 1, 2, 2, 1 and 3 matches. Now the honest part: no pattern understands meaning, so review three cases by hand. Feet and inches (6'2" became 6’2”, where typographers use prime marks), leading apostrophes such as rock 'n' roll (the first one became ‘ instead of ’), and code or HTML (an attribute such as href="x" was turned into curly quotes, which breaks it). To go the other way, the tool has a ready-made preset, "Curly quotes → straight quotes".

The cookbook: dates, numbers and names

Reformat dates with capture groups

Find \b(\d{4})-(\d{2})-(\d{2})\b, replace with $2/$3/$1. Each pair of brackets is a numbered group, and the replacement puts them back in a new order.

  • Before: Draft due 2026-11-04, final 2026-12-02.
  • After: Draft due 11/04/2026, final 12/02/2026. (2 matches)

Named groups make longer patterns easier to read: find (?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2}) and replace with $<m>/$<d>/$<y> for the same result. The built-in "Dates" preset produces day-first dates (14/10/2026) instead.

US dates to ISO, with zero padding

Going from 11/4/2026 to 2026-11-04 needs a leading zero only on one-digit months and days. Two rules do it:

  1. Find \b(\d{1,2})/(\d{1,2})/(\d{4})\b, replace with $3-0$1-0$2 (always add a zero).
  2. Find -0(\d{2})\b, replace with -$1 (remove the zero again where it made three digits).
  • Before: Signed 11/4/2026 and 2/9/2026.
  • After: Signed 2026-11-04 and 2026-02-09.

Make sure you know which order the source uses before converting. 04/11/2026 is April 11 in the US and 4 November in most of Europe, and a regex cannot tell them apart.

Spell out percent signs

Find (\d+)%, replace with $1 percent: "Up 5% and 12%." becomes "Up 5 percent and 12 percent." Useful when a house style asks for the word.

Add a serial comma to a simple list

Find (\w+), (\w+) and (\w+), replace with $1, $2, and $3: "red, white and blue" becomes "red, white, and blue". It only covers three one-word items, so treat it as a search aid and check each match.

Put a currency sign in front of a number

Find (\d+) USD, replace with $$$1: "Total: 15 USD" becomes "Total: $15". The first $$ is a literal dollar sign and $1 is the number. \$$1 works too.

Swap "Last, First" to "First Last"

The "Last, First → First Last" preset uses \p{L}, so accented and apostrophe names work: "Smith, John" becomes "John Smith" and "O’Brien, Mary Ann" becomes "Mary Ann O’Brien", one name per line.

Strip footnote markers

Find ?\[\d+\], replace with nothing: "Results vary.[1] Some studies disagree [23]." becomes "Results vary. Some studies disagree." The optional space before the bracket is removed with it, so no double space is left behind.

Find repeated words such as "the the"

Find (?<!\p{L})(\p{L}+)\s+\1(?!\p{L}), replace with $1. The group captures a word, \1 requires the same word again, and the lookarounds make sure both are whole words.

  • Before: Un café café noir. The the end.
  • After: Un café noir. The end. (2 matches; "The the" is found because matching ignores case)

The shorter pattern you will see in many tutorials, \b(\w+)\s+\1\b, finds nothing in "café café", because \w and \b only know the letters A–Z in JavaScript. Use \p{L} for anything beyond plain English.

A regex also has no judgment. On "She had had enough of the the noise." it returns "She had enough of the noise.", which deletes a correct past perfect along with the typo. It also joins words across a line break. For proofreading, the Duplicate Word Finder is the better tool: it flags pairs such as "had had" and "that that" as often correct, never removes them automatically, and keeps your line breaks.

A smart cleanup in four rules

Stacked rules are where a multi-rule tool beats a single find box. This set tidies text pasted from a PDF or an email:

OrderFindReplaceMatches
1[ \t]+$(nothing)2
2 {2,}one space3
3 +([,.;:!?])$12
4\n{3,}\n\n1

Before (the first two lines end with spaces):

Hello  world ,  this is   a test .  
Next line   



End

After:

Hello world, this is a test.
Next line

End

Tick "Remember my rules in this browser" and the set is still there next time. Only the rules are stored, on your device; your text never is.

Escaping: when a character means something else

In a regex, these characters have special meanings: \ ^ $ . | ? * + ( ) [ ] { }. To match one literally, put a backslash in front of it.

The dot is the classic trap. With Regex ticked, 1.5 matches both "1.5" and "105" in "Version 1.5 vs 105" (2 matches), because the dot means "any character". 1\.5 matches only "1.5". If you do not need a pattern at all, untick Regex: plain rules treat every character literally, so 1.5 finds only 1.5.

A literal backslash is \\. Find \\ and replace with / to turn C:\temp\file into C:/temp/file. You never need to escape the forward slash, because the Find box is not written between slashes as it would be in JavaScript code.

Two more surprises worth knowing:

  • Case and Unicode classes. Because matching ignores case by default, \p{Lu} (capital letters) matched all 25 letters in "Ünal, Zoë and Émile wrote 3 notes." With Match case ticked it matched only Ü, Z and É.
  • Invisible characters. Text copied from the web can contain non-breaking spaces or zero-width characters that a normal space in your pattern will not match. Our guide to invisible characters shows how to find them first.

Patterns the tool refuses, and how to rewrite them

Some patterns can take an astronomically long time on certain inputs, a problem known as catastrophic backtracking. The tool checks every pattern before searching and refuses two shapes:

  • (a+)+$ gets "Pattern too complex: it repeats a group that already repeats (like (a+)+), which can freeze the page." Rewrite it as a+$.
  • (\w|\d)+ gets a message that a repeated group has alternatives that match the same text. Every digit is already a \w, so \w+ does the same job.

An invalid pattern, such as an unclosed bracket in (abc, shows "Invalid regular expression" under the rule, and nothing is changed.

Rule order, swaps and keep case

Order changes results. Rule 1 replaces "colour" with "color", rule 2 replaces "color" with "hue": "colour and color" ends up as "hue and hue", because rule 2 sees rule 1's output.

Swapping two words needs a placeholder. Replace "cats" with @@T@@, then "dogs" with "cats", then @@T@@ with "dogs": "cats chase dogs; dogs chase cats" becomes "dogs chase cats; cats chase dogs".

Whole word stops partial matches. Replacing "cat" with "dog" in "The cat sat on the catalog by the bobcat." gives "dogalog" and "bobdog" without it, and changes only the first "cat" with it.

Keep case copies capitalization. With Keep case and Whole word on, colour → color turns "Colour matters. COLOUR codes. The colour wheel." into "Color matters. COLOR codes. The color wheel." To change capitalization itself, use the Case Converter.

A short checklist

  • Untick Regex when you only need a literal phrase.
  • Escape . ? * + ( ) [ ] { } ^ $ | and backslash when you mean the character itself.
  • Use \p{L} instead of \w for words with accents or in other scripts.
  • Read the match count and the preview before Replace All.
  • Put cleanup rules in a logical order: line ends first, then spaces, then punctuation, then blank lines.
  • Review quotes, measurements and code by hand after an automatic pass.

Open Find and Replace Text, paste a copy of your document, and try the recipes one rule at a time.

Frequently Asked Questions

Which regex flavor does WritePadPro’s Find and Replace use?

JavaScript (ECMAScript) regular expressions, the same syntax browsers use. Multiline mode is always on, so ^ and $ match at every line start and end, matching ignores case unless you tick Match case, and Unicode property escapes such as \p{L} are supported. The dot does not match line breaks; use [\s\S] for any character.

How do I use $1 in the replacement?

Tick Regex, put brackets around the part of the pattern you want to keep, and write $1 in the Replace box for the first group, $2 for the second and so on. For example, find (\d{4})-(\d{2})-(\d{2}) and replace with $2/$3/$1 to turn 2026-11-04 into 11/04/2026. $& inserts the whole match and $$ a dollar sign.

Why does my pattern change more text than I expected?

Usually because a special character is doing its special job. A dot matches any character, so 1.5 also matches 105; escape it as 1\.5. Matching also ignores case by default, so a pattern for capital letters matches lowercase ones too until you tick Match case. Check the match count before replacing.

Can I undo a Replace All?

Yes. After Replace All, the Undo and Redo buttons step backward and forward through each change. The preview also shows every deletion and insertion before you replace anything, and you can copy or download the result once you are happy with it.

Why was my regular expression rejected as too complex?

Patterns that repeat a group which already repeats, such as (a+)+, or that repeat alternatives able to match the same text, such as (\w|\d)+, can take an extremely long time on some inputs. The tool refuses them before searching. Rewrite them without the nested repetition, for example a+ or \w+.

Is my text saved or uploaded when I use regex rules?

No. Searching and replacing run in your browser and the text is never uploaded or stored. Only if you tick Remember my rules in this browser are the rules themselves saved, and then only on your device.

Try These Tools

Related Articles