escapecat
No. 01, on escaping ECMAScript 2025 22.2.5.1 RegExp.escape 1.0 KB gzip

Escape anything.Match it exactly.

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.

Try the playground
npm i escapecat { copied ? 'Copied' : '' }
Read the docs
Live specimen(cats) && [dogs]
escape()\(cats\)\x20\x26\x26\x20\[dogs\]
ProofMatches the input literally
01Playgroundescapecat

Try it

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.

Examples
Flags
Proofescape(input)
native RegExp.escape { ec.same(input) ? 'identical' : 'different' }

This browser has no native RegExp.escape, so escapecat is doing all the work.

Proof
{ ec.pattern(input, flags) }{ ec.literal(input, flags) ? 'matches the input exactly' : 'does not match' }
{ ec.classPattern(input, flags) }{ ec.inClass(input, flags) ? 'works inside a character class' : 'fails inside a character class' }
Code point by code point
{ t.src }{ t.out }
z z Nothing to escape yet. Type something above.
02SpecimenU+0020 to U+007E

The specimen sheet

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.

Unicode
{ ec.spec(sel).code }
Elsewhere
{ ec.spec(sel).mid }
At the start
{ ec.spec(sel).first }

{ ec.spec(sel).kind }

03Featuresescapecat

Small surface, careful details

1,114,112code points checked against native
1.0 KBgzipped
0dependencies
100%line coverage in tests
3.1

Spec exact

Follows ECMAScript 2025 section 22.2.5.1 step by step, including lone surrogates and every Unicode space.

3.2

Native first

Uses the engine's RegExp.escape when present, after checking it against the spec on a probe string.

Path used in this browser
RegExp.escape
escapecat
3.3

Tiny

About 1 KB gzipped. Zero dependencies. Tree shakeable named exports.

3.4

Safe in every context

Works after \0, backreferences and \c, inside classes, with the u and v flags and inside regex literals.

3.5

ESM, CommonJS, types

import or require it. Hand written TypeScript declarations, Node 18 and later, Bun, Deno and browsers.

index.js / index.cjs / index.d.ts

3.6

Optional install()

Want the global? install() adds RegExp.escape only when it is missing, shaped like the built in.

04The problemescapecat

Why it exists

1 5 \.
Fig. 1. In new RegExp('1.5') the dot matches any character, so 1x5 matches too.

The standard finally has it

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.

Hand rolled escapers are subtly wrong

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.

Same output as native, proven

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
05Docsnpm i escapecat

Use it

How to escape a string for a RegExp

  1. Install the package from npm.npm i escapecat
  2. Import escape from escapecat (ESM or CommonJS).import { escape } from 'escapecat';
  3. Pass escape(text) to new RegExp(). The pattern now matches the text literally, with any flags.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

API

escape(string: string): string
Returns pattern text that matches string literally. Throws a TypeError when the argument is not a primitive string (no coercion).
install(): boolean
Adds RegExp.escape when the engine does not have it and returns true. Returns false and changes nothing otherwise.
06Benchmarksops/s

Benchmarks

Operations per second, higher is better. Median of 7 runs.

Short plain text (12 chars)escapecat (native path)escapecat (native path): 12,128,180 ops/s12.1MRegExp.escape (native)RegExp.escape (native): 12,232,740 ops/s12.2Mescapecat (portable)escapecat (portable): 24,121,011 ops/s24.1Mescape-string-regexpescape-string-regexp: 8,209,631 ops/s8.2Mlodash.escaperegexplodash.escaperegexp: 55,520,779 ops/s55.5MShort text with syntax (16 chars)escapecat (native path)escapecat (native path): 9,138,748 ops/s9.1MRegExp.escape (native)RegExp.escape (native): 9,306,167 ops/s9.3Mescapecat (portable)escapecat (portable): 10,146,752 ops/s10.1Mescape-string-regexpescape-string-regexp: 3,252,972 ops/s3.3Mlodash.escaperegexplodash.escaperegexp: 3,579,696 ops/s3.6MURL (60 chars)escapecat (native path)escapecat (native path): 3,666,178 ops/s3.7MRegExp.escape (native)RegExp.escape (native): 3,611,068 ops/s3.6Mescapecat (portable)escapecat (portable): 3,631,608 ops/s3.6Mescape-string-regexpescape-string-regexp: 3,022,126 ops/s3.0Mlodash.escaperegexplodash.escaperegexp: 4,262,644 ops/s4.3MProse (10 KB)escapecat (native path)escapecat (native path): 23,777 ops/s24kRegExp.escape (native)RegExp.escape (native): 22,485 ops/s22kescapecat (portable)escapecat (portable): 20,367 ops/s20kescape-string-regexpescape-string-regexp: 47,801 ops/s48klodash.escaperegexplodash.escaperegexp: 61,814 ops/s62kPlain identifier text (10 KB)escapecat (native path)escapecat (native path): 92,622 ops/s93kRegExp.escape (native)RegExp.escape (native): 86,673 ops/s87kescapecat (portable)escapecat (portable): 49,265 ops/s49kescape-string-regexpescape-string-regexp: 1,347,580 ops/s1.3Mlodash.escaperegexplodash.escaperegexp: 2,762,693 ops/s2.8M

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.

Size

The ESM entry with comments and indentation stripped:

source2.33 KB
gzip1.0 KB
brotli0.91 KB

Compatibility

RuntimePath used
Node 24+native RegExp.escape
Node 18, 20, 22portable implementation
Bun 1.3portable (its built in escapes some code points above U+FFFF)
Deno 2native when present, portable otherwise
Modern browsersnative when present, portable otherwise
07Comparisonescape-string-regexp, lodash.escaperegexp

Compared with other escapers

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.

Inputescapecatescape-string-regexplodash.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

What goes wrong

  1. new RegExp('\\0' + e('1')) becomes an octal escape and stops matching NUL followed by 1.
  2. new RegExp('[' + e('a-z') + ']') with lodash builds the range a to z, so it matches b.
  3. new RegExp('[' + e('&&') + ']', 'v') throws a SyntaxError with both.
  4. A slash or a line break in the input breaks a generated regex literal.
08FAQescapecat

Questions

Why not just use RegExp.escape?

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.

Why is the first letter escaped as \x61?

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.

Is it a polyfill?

It is a ponyfill: importing it changes nothing global. Call install() if you want RegExp.escape defined.

Do I need a separate function for character classes?

No. The output of RegExp.escape is already valid inside [...], with or without the u and v flags, so escapecat ships no extra helper.

Are the error messages the same as native?

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.

Ready to escape?

One install, one function, the exact output of the standard. Your patterns match what you meant, nothing more.

npm i escapecat { copied ? 'Copied' : '' }
GitHub npm