Regex Escape

Escape a string for use in regex.

Developer

The Regex Escape tool takes a plain string and escapes all characters that have special meaning in regular expressions, so the resulting pattern matches your text literally. Regular expression metacharacters like . * + ? [ ] ( ) { } | \ ^ $ trigger pattern logic instead of matching themselves unless properly escaped. This tool saves you from manually adding backslashes and risking missed characters. Paste your string, click escape, and get a regex-safe version instantly. All processing runs in your browser — your text is never sent to any server.

About Regex Escape

In regular expressions, characters like ., *, +, ?, [, ], (, ), {, }, |, \, ^, and $ have special meanings. If you want to search for these characters literally — for example, matching the string price (USD) — each special character must be preceded by a backslash. Manually escaping is tedious and error-prone. This tool automates the process, ensuring every metacharacter is properly escaped so your pattern works as intended across JavaScript, Python, PHP, and other regex engines.

How to Use Regex Escape

  1. Enter the string you want to match literally in a regular expression.
  2. Click Escape for regex.
  3. Copy the escaped result and use it in your regex pattern — in code, an editor search, or a Regex Tester.

Key Features

  • Escapes all standard regex metacharacters with backslashes
  • Produces output compatible with JavaScript, Python, PHP, Java, and most regex engines
  • Prevents accidental pattern behavior from unescaped special characters
  • Instant, one-click escaping
  • 100% client-side — your text never leaves your browser

When to Use This Tool

  • Building a regex that must match user-supplied text literally
  • Inserting file paths, URLs, or code snippets into a regex pattern
  • Constructing dynamic regex patterns in code (e.g. new RegExp(escaped))
  • Preventing regex injection when user input becomes part of a search pattern
  • Learning which characters are special in regex syntax

Technical Details

The tool applies a regular expression replacement that targets all metacharacters: [-\/\\^$*+?.()|[\]{}] and prepends each with a backslash. This is the same approach used by libraries like lodash.escapeRegExp and Python's re.escape(). The escaped output is safe to use in new RegExp() in JavaScript, re.compile() in Python, and similar constructors in other languages. Test the result with our Regex Tester.

Conclusion

Properly escaping special characters is essential for correct and secure regex patterns. This free, browser-based Regex Escape tool handles it instantly — saving you time and preventing hard-to-debug pattern errors.

Frequently Asked Questions

Which characters are escaped?
All standard regex metacharacters: . * + ? ^ $ [ ] ( ) { } | \ and the hyphen. Each is prepended with a backslash so the pattern matches your string literally.
Does the output work in all programming languages?
Yes. The escaped output is compatible with JavaScript, Python, PHP, Java, Ruby, Go, and most other regex engines that follow standard metacharacter conventions.
Can I use this in JavaScript's new RegExp()?
Absolutely. Use the escaped string in new RegExp(escaped) or template it into a regex literal. The special characters will be treated as literal matches.
Is my text sent to a server?
No. Escaping runs entirely in your browser using JavaScript. Your text never leaves your device.