⌨ DEVELOPER · ENCODER

Regex Escape

Make any text safe to use inside a regular expression. Characters like . * + ? ( ) [ ] { } ^ $ | are escaped with the exact rules of your language — JavaScript, Python, PHP, Java, .NET or Go — so the pattern matches the text literally.

6 languages · Inside or outside [ ] · Pattern or code string · Line-by-line & alternation
Tip: escaping a list of words? Turn on Each line separately and Join lines with | to get word1|word2|word3.Testing the pattern next? Regex Tester →
Literal text Plain text
Escaped regex
Input: –
Escaped: –
Output: –
Time: –
Per-language rules · 100% Client-Side
◆ ABOUT THIS TOOL

Why Escape Text for a Regex?

Some characters have a special meaning in regular expressions.

In a regular expression, . means “any character”, * means “repeat”, ( ) make a group and so on. If you build a pattern from user input or a fixed string — say, to find $9.99 — those characters must be escaped with a backslash, or the regex will match the wrong things or fail to compile.

Each language escapes slightly differently. This tool follows the actual rules of each one: re.escape in Python 3.7+, preg_quote in PHP 7.3+, Regex.Escape in .NET and regexp.QuoteMeta in Go. For JavaScript and Java it escapes exactly the regex syntax characters, so the JavaScript result works with or without the u and v flags.

Inside [ ]. Inside a character class, only \ ] [ ^ - are special (plus & in Java, and ( ) { } / | with JavaScript's v flag). Choose Inside [ ] to escape just those. With the v flag, also avoid doubled punctuation such as && inside [ ] — it's reserved there.

Code string. In Java, C# and other languages a regex is usually written inside a string literal, so the backslashes must be escaped again. Code string gives you a ready-to-paste literal: /…/ for JavaScript, r'…' for Python, '/…/' for PHP, "…" for Java, @"…" for C# and `…` for Go.

⌘ REFERENCE

What Each Language Escapes

Characters escaped in “In a pattern” mode.

LanguageEscaped charactersBuilt-in equivalent
JavaScript\ ^ $ . * + ? ( ) [ ] { } | /Safe for literals, new RegExp() and the u / v flags
Python( ) [ ] { } ? * + - | ^ $ \ . & ~ #, space and \t \n \r \v \fre.escape() (3.7+)
PHP. \ + * ? [ ^ ] $ ( ) { } = ! < > | : - # /, NUL → \000preg_quote($s, '/')
Java\ ^ $ . | ? * + ( ) [ ] { }Same result as Pattern.quote()
.NET / C#\ * + ? | { [ ( ) ^ $ . #, space → \ , tab/newline → \t \n \r \fRegex.Escape()
Go\ . + * ? ( ) | [ ] { } ^ $regexp.QuoteMeta()
◐ THREE STEPS

How to Use the Regex Escape

Get your result in under ten seconds.

1

Paste the literal text

The exact text the regex should match.

2

Pick language &amp; position

Your regex flavour, and whether it goes inside [ ].

3

Copy the pattern

As a bare pattern, or as a ready-to-paste code string.

✓ FEATURES

Why Use the Regex Escape on Developers Web Tools

Fast, accurate and private — built for developers.

6

Six flavours

JavaScript, Python, PHP, Java, .NET and Go — each with its own exact rules.

[ ]

Character classes

A separate mode that escapes only what's special inside [ ].

"…"

Code strings

Backslashes doubled where the language needs it — no more \\\\ guesswork.

|

Word lists

Escape many lines at once and join them into one alternation.

✓

Verified

Outputs are tested against the languages' own escape functions.

🔒

Private

Runs entirely in your browser.

FAQ

Frequently Asked Questions

Outside a character class: \ ^ $ . | ? * + ( ) [ ] { } in almost every flavour. Inside [ ]: only \ ] ^ - (and [ in some flavours). Some languages escape more, such as / in JavaScript literals or # and spaces in Python and .NET.

With JavaScript's u or v flag, escaping a character that isn't special (like \- outside a class) is a syntax error. This tool escapes only syntax characters, so the result works with and without those flags.

In Java source code, the regex is inside a string literal, and string literals use backslash escapes too. The regex \. must be written as "\\.". Choose Code string to get that automatically.

It matches exactly the same text. Pattern.quote() wraps the string in \Q…\E; this tool uses backslashes, which is easier to read and to combine with other pattern parts.

Yes: turn on Each line separately and Join lines with |. Put longer words first if one word is the start of another (e.g. cats|cat).

No. Everything runs in your browser.