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
- Paste the literal text.
- Press Run.
- Copy the result into your pattern.
Example
precio (USD): $9.99 + IVA [21%]? 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.