Skip to content

About

Modern escape-string-regexp replacement — escape regex special chars. TypeScript, ESM + CJS, zero deps.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

tiny-escape

npm version npm downloads CI TypeScript License: MIT

Escape special characters in a string for use in a regular expression. Same behavior as escape-string-regexp, but ships both ESM and CJS.

import { escapeRegExp } from "tiny-escape";

const input = "price is $5.00 (USD)";
new RegExp(escapeRegExp(input)).test(input); // true

201 bytes gzipped. Zero dependencies.

Demo

Demo built with remotion-readme-kit

Install

npm install tiny-escape

Usage

import { escapeRegExp } from "tiny-escape";

escapeRegExp("hello.world"); // "hello\\.world"
escapeRegExp("[test] (1+1)"); // "\\[test\\] \\(1\\+1\\)"
escapeRegExp("foo|bar"); // "foo\\|bar"
escapeRegExp("a-b"); // "a\\x2db"

const userInput = "How much $ for mass?";
const re = new RegExp(escapeRegExp(userInput), "i");
re.test(userInput); // true

Differences from escape-string-regexp

escape-string-regexp v5 is ESM-only. If you require("escape-string-regexp") you get ERR_REQUIRE_ESM. tiny-escape works with both import and require().

escape-string-regexp tiny-escape
CJS support v4 only (v5 ESM-only) ESM + CJS
TypeScript native (v5) native
Export default named

Migrating from escape-string-regexp

- import escapeStringRegexp from "escape-string-regexp";
- const escaped = escapeStringRegexp(input);
+ import { escapeRegExp } from "tiny-escape";
+ const escaped = escapeRegExp(input);

API

escapeRegExp(string: string): string

Escapes | \ { } ( ) [ ] ^ $ + * ? . and -.

Throws TypeError if the input is not a string.

The tiny-* family

Drop-in replacements for sindresorhus async utilities. All ship ESM + CJS with zero dependencies.

Package Replaces What it does
tiny-limit p-limit Concurrency limiter
tiny-map p-map Concurrent map with order
tiny-retry p-retry Retry with exponential backoff
tiny-queue p-queue Priority task queue
tiny-ms ms Parse/format durations
tiny-escape escape-string-regexp Escape regex chars

Want all async utilities in one import? Use tiny-pasync.

Author

Made by ofershap

LinkedIn GitHub


If this saved you from ERR_REQUIRE_ESM, star the repo or open an issue if something breaks.


README built with README Builder

License

MIT © Ofer Shapira

About

Modern escape-string-regexp replacement — escape regex special chars. TypeScript, ESM + CJS, zero deps.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages