Regex Escape Tool
Paste text to escape its regex special characters.
Result
How it is calculated
The tool walks through your text one byte at a time and puts a backslash in front of every character from this fixed list: . ^ $ * + ? ( ) [ ] { } | \ /. Everything else is copied unchanged, including letters, digits, spaces, accented letters and emoji. The slash is on the list even though it is only special inside JavaScript regex literals. The hyphen - and the hash # are not escaped, so a hyphen inside a character class such as [a-z] still keeps its range meaning. The output also reports how many characters were escaped and the length of the original text counted in characters, not bytes. Empty text shows no result, while text made only of spaces is returned as it is. The result is intended for PHP, Python, JavaScript and similar engines, but check the flavour you use.
Worked example
Take the text price: $19.99 (USD) [net]. It has 25 characters. Six of them are metacharacters: the dollar sign, the full stop, the two round brackets and the two square brackets. Each gets a backslash in front, the letters, digits, colon and spaces are left alone, and the result is price: \$19\.99 \(USD\) \[net\]. The tool reports 6 characters escaped out of 25 characters. Used as a pattern, it now matches the original text literally instead of treating the dot as "any character" and the brackets as groups.
Common mistakes
- Assuming the hyphen is escaped. It is not, so if you place the output inside a character class such as
[...], escape any hyphen yourself or put it first or last. - Escaping the same text twice. Running the output through the tool again doubles every backslash, which then matches a literal backslash followed by the character.
- Pasting the result into a language string without doubling the backslashes. In a normal Java, C or JavaScript string literal,
\.must be written\\.unless you use a raw string. - Using the output for the replacement side of a find-and-replace. Only the pattern side needs this escaping; replacement strings follow different rules (such as
$1). - Expecting whitespace, newlines or the hash to be treated specially. They pass through unchanged, so in free-spacing mode (where spaces and
#have meaning) you must escape them yourself.
About this calculator
Regular expressions treat certain characters as metacharacters with special meaning — a period matches any character, an asterisk means zero or more, parentheses group patterns, and so on. If you want to search for those characters literally (a period in a filename, a dollar sign in a price), you have to escape each one with a backslash so the regex engine treats it as plain text instead of an instruction.
This tool automatically prefixes every metacharacter in your input — including the period, asterisk, plus, question mark, parentheses, brackets, braces, pipe, caret, dollar sign, and backslash itself — with a backslash, producing a string safe to drop into any regex pattern. It's equivalent to what functions like JavaScript's RegExp.escape, Python's re.escape, or PHP's preg_quote do programmatically.
Developers reach for this when building a regex dynamically from user input — like a search box — where an unescaped special character would either throw a syntax error or silently match the wrong things.
Was this helpful?
You can pick up to 3 reactions.
Popular calculators
Loan Calculator
Monthly payment, total interest and full amortization schedule.
Percentage Calculator
Percent change, percent of, and ratio calculations.
Discount Calculator
Calculate final price, savings, and discount amount.
BMI Calculator
Body mass index from height and weight (metric or imperial).
Age Calculator
Compute your exact age in years, months and days from a birth date.
Compound Interest Calculator
Calculate the final amount and interest earned with compounding.
All Calculators
- Loan Calculator
- Profit Calculator
- Discount Calculator
- BMI Calculator
- Percentage Calculator
- Ideal Weight Calculator
- Age Calculator
- Pregnancy Due Date Calculator
- Compound Interest Calculator
- BMR Calculator (Basal Metabolic Rate)
- Tip Calculator
- Fuel Cost Calculator
- Date Difference Calculator
- Sleep Cycle Calculator
- Daily Water Intake Calculator
- Simple Interest Calculator
- Heart Rate Zones Calculator
- Running Pace Calculator
- Pet Age Calculator
- Calories Burned Calculator
- Macro Calculator
- TDEE Calculator (Total Daily Energy Expenditure)
- Waist-to-Hip Ratio Calculator
- Honor Roll Calculator
- One Rep Max (1RM) Calculator
- Ovulation Calculator
- Electricity Cost Calculator
- Unit Price Calculator
- Savings Goal Calculator
- Break-Even Point Calculator
- Paint Calculator
- Course Grade Average Calculator
- Business Days Calculator
- Time Duration Calculator
- Fuel Economy Calculator
- Temperature Converter
- Tile Calculator
- Aspect Ratio Calculator
- Roman Numeral Converter
- Goal Weight Timeline Calculator
- Salary Converter (Hourly to Annual)
- Credit Card Validator (Luhn)
- Body Surface Area (BSA) Calculator
- Feels-Like Temperature Calculator
- Ohm’s Law Calculator
- PPI (Pixel Density) Calculator
- Molar Mass Calculator
- Zodiac Sign Calculator (Western + Chinese)
- IBAN Validator
- Shoe Size Converter
- Country Info & Dialing Code Lookup
- Time Zone Difference (World Clock)
- CAGR (Compound Annual Growth Rate) Calculator
- Rule of 72 Calculator
- Blood Alcohol Content (BAC) Calculator
- Lean Body Mass (LBM) Calculator
- Pythagorean Theorem Calculator
- Speed Converter
- Download Time Calculator
- Date Calculator (Add/Subtract)
No calculators found
Comments