escape

import { escape } from "jsr:@ende/regexp";

function

function escape(str: string): string

Escapes arbitrary text for interpolation into a regexp, such that it will match exactly that text and nothing else.

str: string

The string to escape.

Returns

The escaped string.

Example: Usage

import { escape } from "@ende/regexp/escape";
import { assertEquals, assertMatch, assertNotMatch } from "@ende/assert";

const re = new RegExp(`^${escape(".")}$`, "u");

assertEquals("^\\.$", re.source);
assertMatch(".", re);
assertNotMatch("a", re);

Source