About Regex Escape
A file name, a price or a line of code is full of characters that mean something to a regular expression: dots, brackets, question marks, dollar signs. Paste the text and get back a pattern that matches it literally, ready to drop into a larger expression.
For JavaScript you can take the result as a bare pattern, a /literal/ or a quoted string for new RegExp(). For PCRE, Python, Java, .NET and Go every ASCII punctuation mark is escaped, which all of them accept. Turn on Each line is an alternative to match any one of a list of words or phrases.
How it works
- For JavaScript only \ ^ $ . * + ? ( ) [ ] { } | and / are escaped. Outside brackets those are the only punctuation marks the u and v flags allow after a backslash, so the result works with every flag.
- For other engines every ASCII punctuation mark except the underscore gets a backslash. Letters and digits never do, since a backslash turns them into classes like \d or back-references, and spaces are left alone because Go rejects an escaped space.
- Line breaks, tabs and other control characters become \n, \t or \xHH, so the pattern stays on one line.
- With Each line is an alternative, repeated and empty lines are dropped and the rest are joined as (?:a|b|c), longest first, so “category” is not cut short by “cat”.
Questions
Why are some characters, like the hyphen, not escaped for JavaScript?
Outside square brackets a hyphen, comma or hash already matches itself, and JavaScript with the u or v flag rejects a backslash before them. Leaving them alone keeps the pattern valid under every flag.
Can I use the result inside square brackets?
It is meant for use outside them. Inside a character class different characters are special, so build classes by hand.
What is the String form for?
When a pattern is built with new RegExp("…") the text passes through a string literal first, so every backslash must be doubled. The String form does that and adds the quotes.
Is my text sent anywhere?
No. Escaping is a simple character-by-character rewrite done on this page, and the text never leaves your browser.
Everything you enter here is processed in this browser tab. Nothing is uploaded, logged or shared. How privacy works here.