Regex Escape

Turn text into a safe regular expression literal by escaping . * + ? ^ $ { } ( ) | [ ] \ and /.

Input

Ctrl + Enter to run

Result

Turn any text into a safe literal for use inside a regular expression: it escapes characters with special meaning (\ ^ $ . * + ? ( ) [ ] { } |) and the slash /.

The result is valid with or without the u flag, and inside or outside a character class.

How to use it

  1. Paste the literal text.
  2. Press Run.
  3. Copy the result into your pattern.

Example

Example input
precio (USD): $9.99 + IVA [21%]?
Result
precio \(USD\): \$9\.99 \+ IVA \[21%\]\?

Limitations

  • The hyphen “-” is not escaped because with the u flag that would be an error outside a class; inside [ ] place it first or last.

FAQ

Is it the same as RegExp.escape?
Same purpose. The newer RegExp.escape escapes more characters (for example a leading digit); this output also works in older browsers.
Why escape the slash?
So you can paste the result into a JavaScript /…/ literal without closing it early.

Related tools

References