Bulk Replace
Regex

How to Escape Special Characters in a Regex Find and Replace

Which characters need escaping in regex, how to escape them in the find and the replace boxes, and quick ways to match a literal URL, path or price.

Updated 5 minute read The Bulk Replace team

To escape a special character in regex, put a backslash in front of it. In the find box, the characters that need escaping are . ^ $ * + ? ( ) [ ] { } | and the backslash itself, so example.com becomes example\.com and C:\Data becomes C:\\Data. The replace box has different rules: in .NET, PowerShell and VS Code, only $ is special and you write $$ for a literal dollar sign. If you do not need any pattern features at all, the simplest fix is to switch the rule to plain-text mode, where nothing needs escaping.

This guide lists what to escape in each box, gives ready-made examples for URLs, paths and prices, and shows a function that escapes text for you.

Characters that need escaping in the find pattern

CharacterRegex meaningLiteral form
.Any character\.
* + ?Repeat the previous item\* \+ \?
^ $Start and end of text or line\^ \$
( )Group\( \)
[ ]Character class\[ \]
{ }Repeat count, as in \d{4}\{ \}
|Or\|
\Escape character\\

Some characters only need escaping in certain places. A closing ] or } on its own is treated literally by many engines, but escaping it is always safe. When in doubt, escape any symbol that is not a letter, a digit or a space. Escaping punctuation that did not need it is usually harmless, but never put a backslash before a letter or digit unless you mean it: \d, \b and \1 all have their own meanings.

Characters that never need escaping in the find pattern include letters, digits, spaces, , - (outside a class) : ; = @ % & ' " and /. The forward slash only needs escaping in languages that use it as a delimiter, such as JavaScript regex literals, not in a search box.

Inside a character class

Inside square brackets, most special characters lose their meaning. [.?*+] matches a dot, a question mark, an asterisk or a plus, with no backslashes. Only four characters need care:

  • ] closes the class, so write \].
  • \ is still the escape character, so write \\.
  • ^ at the start negates the class. Put it later, or write \^.
  • - makes a range. Put it first or last, as in [-a-z] or [a-z-], or write \-.

Common literal text, escaped

Text to matchRegex
www.example.comwww\.example\.com
https://example.com/page?id=5https://example\.com/page\?id=5
C:\Users\PublicC:\\Users\\Public
\\server\share\\\\server\\share
$19.99\$19\.99
getData()getData\(\)
[DRAFT]\[DRAFT\]
{{name}}\{\{name\}\}
1+1=21\+1=2
a|ba\|b
*.txt\*\.txt

Mixing escaped and unescaped parts is where regex earns its keep. To match any price, escape the dollar and the dot but keep the pattern parts: \$\d+\.\d{2}.

Let the computer escape it: [regex]::Escape

In PowerShell, [regex]::Escape() returns any text with its special characters escaped:

[regex]::Escape('C:\Data\file (1).txt')

returns C:\\Data\\file\ \(1\)\.txt. The .NET version also escapes spaces and #, which is harmless. Use it whenever you build a pattern from a variable, for example:

$old = [regex]::Escape('price: $5.00'); $text -replace $old, 'price: $$6.00'

Note the doubled dollar in the replacement. That is the replacement-side rule, covered next.

Escaping in the replacement

The replacement is not a regex, so dots, brackets and question marks are always literal there. Each engine has its own short list of special characters:

ToolSpecial in replacementLiteral dollarLiteral backslash
.NET, PowerShell -replace$$$\ (no escape needed)
JavaScript$$$\
VS Code$ and \$$\\
Notepad++$, \, and ( ) in some versions\$\\

So to replace 5.00 with $6.00 in PowerShell, write -replace '5\.00', '$$6.00'. A single $6 would be read as "group 6", which does not exist.

VS Code treats a backslash in the replacement as the start of an escape such as \n for a new line, so a literal backslash needs \\. Notepad++ uses Boost's replacement format, where a backslash introduces escapes and parentheses can be read as part of its conditional syntax, so \( and \) are the safe way to write literal parentheses there.

Escaping in PowerShell strings

PowerShell adds its own layer. In double-quoted strings, $ starts a variable and the backtick is the escape character. Single quotes avoid both problems:

  • Good: -replace '\$(\d+)', 'USD $1'
  • Broken: -replace "\$(\d+)", "USD $1", because PowerShell reads $( as the start of a subexpression and $1 as an empty variable.

Make single quotes your default for both the pattern and the replacement. More on the replacement side is in regex backreferences in the replacement.

When to skip regex and escaping altogether

If the text you are searching for is fixed, plain-text mode is simpler and safer. There is nothing to escape, so a URL with a query string or a path full of backslashes is matched exactly as typed. Use regex only when part of the search varies. Our comparison of wildcards vs regex helps you pick the right mode.

Bulk Replace sets the mode per rule, so one run can mix a plain-text rule for an exact URL with a regex rule for a date pattern. The before/after preview shows exactly what each rule matched, which is the quickest way to spot a dot that should have been escaped. If something does slip through, Bulk Replace keeps a backup of every run for one-click undo.

Worked example: replacing a URL with a query string

Say every page links to https://example.com/shop?ref=old&id=5 and the ref value should become new, while the id varies from page to page. Here is how to build the pattern safely:

  1. Start from the literal text: https://example.com/shop?ref=old&id=.
  2. Escape the dots and the question mark: https://example\.com/shop\?ref=old&id=. The ampersand, slashes and equals signs need nothing.
  3. Add the variable part as a group: (\d+) after id=.
  4. Write the replacement with the group: https://example.com/shop?ref=new&id=$1. No escaping is needed on this side, because dots and question marks are literal in a replacement.
  5. Search first and check the count matches the number of links you expect.

If you forget step 2, the pattern still finds the links, but an unescaped ? makes the letter before it optional, so the pattern would no longer require shop to end in a p. Mistakes like this rarely show up on the first file, which is why the count check matters.

Checklist

  1. Decide whether you need regex at all. If not, use plain text.
  2. In the find pattern, escape . ^ $ * + ? ( ) [ ] { } | \.
  3. Inside a class, watch only ] \ ^ -.
  4. In the replacement, double the dollar sign for a literal $.
  5. In PowerShell, use single quotes, and [regex]::Escape() for variable text.
  6. Check the match count before replacing.

Frequently asked questions

Which characters must be escaped in regex?

The usual list is . ^ $ * + ? ( ) [ ] { } | and the backslash itself. Inside a character class only ] \ ^ and - normally need care.

How do I put a literal dollar sign in the replacement?

In .NET, PowerShell and JavaScript replacements, write $$ to get a single $.

Is there a function that escapes text for me?

Yes. In PowerShell, [regex]::Escape('text') returns the text with every special character escaped.

Get three guides like this every month, plus software update notices.Subscribe →