# Regex Literal Escape

Escape special characters so a regex matches text literally. Nine flavors: PCRE, JavaScript, Python, Go/RE2, .NET, Java, Ruby, and Rust.

## Run it

- **CLI:** `gizza tool regex-literal-escape "a.b*c+(d)"`
- **Web:** https://gizza.ai/tools/regex-literal-escape/
- **Agents:** machine-readable descriptor (parameters JSON Schema) at https://gizza.ai/tools/regex-literal-escape/tool.json

## Inputs

- `text` — Text to escape _(field)_
- `flavor` — Regex flavor _(field)_
- `delimiter` — Also escape (delimiter) _(field)_
- `escape_whitespace` — Escape whitespace (/x extended mode) _(field)_
- `string_literal` — Escape again for a code string literal _(field)_

## Output

- Escaped literal (text)

## Query parameters

Open the tool pre-filled and auto-run via URL:

- `text` — Text to escape
- `flavor` — Regex flavor
- `delimiter` — Also escape (delimiter)
- `escape_whitespace` — Escape whitespace (/x extended mode)
- `string_literal` — Escape again for a code string literal

Example: `https://gizza.ai/tools/regex-literal-escape/?text=a.b%2Ac%2B%28d%29&flavor=pcre&delimiter=%2F&escape_whitespace=true&string_literal=true`

---

## About this tool

Regex Literal Escape turns arbitrary text into a regex-safe literal, so the pattern matches the text itself instead of treating punctuation as regex syntax. It is useful when you interpolate user input into a larger pattern, build search tools, generate deny-list rules, or need to paste a literal URL/path into a regex tester.

Different engines escape different character sets, so the tool includes nine flavors:

- PCRE / PHP `preg_quote()`
- JavaScript's common `new RegExp()` helper
- JavaScript `RegExp.escape()` strict escaping
- Python `re.escape()`
- Go / RE2 `regexp.QuoteMeta()`
- .NET `Regex.Escape()`
- Java `Pattern.quote()`
- Ruby `Regexp.escape()`
- Rust `regex::escape()`

Use `delimiter` when your regex language also needs the pattern delimiter escaped, such as `/` in `/.../`. Turn on `escape_whitespace` for extended/free-spacing modes where a raw space can be ignored. Turn on `string_literal` when you want the escaped regex text double-escaped for a source-code string.

### Worked example

Input:

```text
a.b*c+(d)
```

With `flavor=pcre`, the output is:

```text
a\.b\*c\+\(d\)
```

That pattern matches the literal characters `a.b*c+(d)`, not "a, any character, b repeated, c, one or more...".

### Limits and edge cases

- Input is capped at 100,000 characters.
- Java uses `\Q...\E` quoting rather than backslash-prefixing each character.
- `javascript-strict` follows `RegExp.escape()` behavior and may emit `\xNN` for the first letter or digit and for punctuation that cannot be safely backslash-escaped.
- Alphanumeric, underscore, and whitespace delimiters are rejected because they can form ambiguous regex escapes.

## FAQ

<details>
<summary>Why do different flavors produce different output?</summary>

Regex engines do not all treat punctuation the same way. For example, Go/RE2 leaves `-` and `#` alone, while PCRE and Rust escape them. Pick the flavor that matches the engine where you will run the pattern.

</details>

<details>
<summary>Should I enable string literal escaping?</summary>

Enable it only when you are pasting the result inside quoted source code, such as a JavaScript, C#, Java, or Go string. If you are pasting directly into a regex field or a raw string, leave it off so backslashes are not doubled.

</details>

<details>
<summary>What is the delimiter field for?</summary>

Some regex syntaxes wrap patterns in a delimiter, for example `/literal/` in JavaScript or PHP. If your literal text can contain that delimiter, set `delimiter=/` so the delimiter is escaped too.

</details>

<details>
<summary>Does this validate that my whole regex is correct?</summary>

No. This tool escapes literal text. It does not parse or validate a larger regex. Use the escaped output as one safe literal fragment inside your own pattern.

</details>

## Related tools

- [String Escaper](https://gizza.ai/tools/string-escaper/): Escape (or unescape) any string for JSON, JavaScript, HTML, URL, shell, SQL, or regex. Spec-aware escaping that runs in your browser; nothing is uploaded.
- [Apply a Unified Diff to a File](https://gizza.ai/tools/apply-patch/): Paste a file and a unified diff to get the patched text in your browser, with reverse apply, fuzz matching, and per-hunk conflict reports.
- [Autocomplete Trie](https://gizza.ai/tools/autocomplete-trie/): Build a prefix trie from a pasted wordlist and get ranked autocomplete suggestions for any typed prefix. Weights, typo tolerance, trie stats, JSON. Runs locally.
- [Code Chunker](https://gizza.ai/tools/code-chunker/): Split Python, Rust, JavaScript, TypeScript, Go, Java, C/C++, C#, PHP, or Swift into line-ranged chunks that keep functions and classes intact.
- [Code Formatter](https://gizza.ai/tools/code-formatter/): Beautify and re-indent minified or messy HTML, CSS, JavaScript, or JSON. Auto-detect the language, choose spaces or tabs, and format locally in your browser.
