Escapes every regex metacharacter in a piece of text (. * + ? ^ $ { } ( ) | [ ] \) so it matches literally when you drop it into a pattern, for example a file name, a price or a user-supplied search term.
Flavors:
- JavaScript / Python / Java: the standard metacharacters, optionally
/for regex literals. - PCRE / PHP: matches
preg_quote, which also escapes- # = ! < > :. - POSIX ERE: for
grep -Eandsed -E. - Inside a character class: only
\ ] [ ^ -need escaping there.
Output as quoted string literal doubles the backslashes and wraps the result in quotes, ready to paste into new RegExp("…") or most other languages’ string syntax. Unescape removes backslashes in front of punctuation while leaving escapes like \d and \n alone.