string literally. Throws a TypeError when the argument is not a primitive string (no coercion).escapecat A spec exact ponyfill of the new standard method. Same output, character for character, in every engine. It uses the native function when it exists and gets out of the way.
Type anything. escapecat escapes it, the browser's own RegExp.escape runs next to it when it has one, and the result goes straight into new RegExp to prove it matches your text literally.
This browser has no native RegExp.escape, so escapecat is doing all the work.
Every printable ASCII character, set the way RegExp.escape sets it. Red is always escaped; a red corner is escaped only at the start of a string. Select a character to read its rule.
{ ec.spec(sel).kind }
Follows ECMAScript 2025 section 22.2.5.1 step by step, including lone surrogates and every Unicode space.
Uses the engine's RegExp.escape when present, after checking it against the spec on a probe string.
About 1 KB gzipped. Zero dependencies. Tree shakeable named exports.
Works after \0, backreferences and \c, inside classes, with the u and v flags and inside regex literals.
import or require it. Hand written TypeScript declarations, Node 18 and later, Bun, Deno and browsers.
index.js / index.cjs / index.d.ts
Want the global? install() adds RegExp.escape only when it is missing, shaped like the built in.
ES2025 added RegExp.escape. Node 24 and current browsers ship it, but older Node versions and older browsers do not. escapecat gives you the exact same function today, and steps aside when the engine has a correct one.
Most escapers only handle the obvious characters. They forget that \0 followed by a digit is an octal escape, that / ends a regex literal, or that && is reserved inside a v mode class. The spec algorithm was designed around those traps.
The test suite compares escapecat with the native function over all 1,114,112 code points in three positions, every BMP punctuation character and 200,000 seeded random strings. Zero differences.
Same output, character for character, in every engine. escapecat
npm i escapecatimport { escape } from 'escapecat';new RegExp(escape(text), 'u')import { escape } from 'escapecat'; escape('1.5$'); // '\\x31\\.5\\$' escape('(cats) && [dogs]'); // '\\(cats\\)\\x20\\x26\\x26\\x20\\[dogs\\]' const price = new RegExp(`^${escape('$9.99')}$`); price.test('$9.99'); // true price.test('$9x99'); // false
string literally. Throws a TypeError when the argument is not a primitive string (no coercion).RegExp.escape when the engine does not have it and returns true. Returns false and changes nothing otherwise.Operations per second, higher is better. Median of 7 runs.
lodash.escaperegexp wins on plain text because it returns early when nothing needs escaping, and on long text the regex based libraries are faster, partly because they escape far fewer characters. On short strings with syntax characters, escapecat leads.
Measured on Node v24.16.0, AMD Ryzen 7 7800X3D 8-Core Processor, win32 x64, 2026-12-04. escape-string-regexp 5.0.0, lodash.escaperegexp 4.1.2.
The ESM entry with comments and indentation stripped:
| Runtime | Path used |
|---|---|
| Node 24+ | native RegExp.escape |
| Node 18, 20, 22 | portable implementation |
| Bun 1.3 | portable (its built in escapes some code points above U+FFFF) |
| Deno 2 | native when present, portable otherwise |
| Modern browsers | native when present, portable otherwise |
Real output from escape-string-regexp 5.0.0 and lodash.escaperegexp 4.1.2. They are fine for the simple case but they are not RegExp.escape.
| Input | escapecat | escape-string-regexp | lodash.escaperegexp |
|---|---|---|---|
| "1.5" | "\\x31\\.5" | "1\\.5" | "1\\.5" |
| "a-z" | "\\x61\\x2dz" | "a\\x2dz" | "a-z" |
| "/" | "\\/" | "/" | "/" |
| "&&" | "\\x26\\x26" | "&&" | "&&" |
| " " | "\\x20" | " " | " " |
| "\n" | "\\n" | "\n" | "\n" |
| "\u2028" | "\\u2028" | "\u2028" | "\u2028" |
| "\ud800" | "\\ud800" | "\ud800" | "\ud800" |
same output as RegExp.escape differs from RegExp.escape
new RegExp('\\0' + e('1')) becomes an octal escape and stops matching NUL followed by 1.new RegExp('[' + e('a-z') + ']') with lodash builds the range a to z, so it matches b.new RegExp('[' + e('&&') + ']', 'v') throws a SyntaxError with both.Do, if every engine you support has it. escapecat is for the rest: Node 18 to 22, older browsers and runtimes whose built in disagrees with the spec. On engines with a correct native function, escapecat simply calls it.
So that the result can follow anything. After \c, \0 or a backreference like \1, a bare letter or digit would change the meaning of the escape before it. The spec escapes the first character for that reason.
It is a ponyfill: importing it changes nothing global. Call install() if you want RegExp.escape defined.
No. The output of RegExp.escape is already valid inside [...], with or without the u and v flags, so escapecat ships no extra helper.
The error type is the same (TypeError). On the native path you get the engine's message, on the portable path a message of the form RegExp.escape: argument must be a string, got number.
One install, one function, the exact output of the standard. Your patterns match what you meant, nothing more.