Escape regex special characters

Escape user text so it can be inserted into a regular expression literally.

freeworks offlinenothing uploaded
ToolRegex Escape
Input
Output
Put this on your own site

The frame below runs the same code as this page, in the reader's own browser. Nothing is sent to us, and nothing is sent to you.

Pick a dark background and the text and panels follow it, so the frame stays readable on a dark page.

Preview

How it works

Regex metacharacters receive safe escapes so their operator meaning is removed; the result can be embedded as one literal fragment. The operation protects dynamic patterns from accidental wildcards and injection.

  • Escaping is for literal fragments, not complete expressions.
  • Backslashes need another escaping layer inside string literals.

Worked example

Escape URL special characters for regex
Escape special regex characters in a URL for literal matching
Input
											https://example.com/path?q=1&r=2
										
Output
												https://example\.com/path\?q=1&r=2
											

When to use this

Search boxes, log filters, and fixture-generated patterns escape user text.

Edge cases

  • Hyphens inside character classes need position-aware handling.
  • A backslash must survive the host language string parser.
  • Escaping an entire expression disables intended operators.

References