Escapes arbitrary text for interpolation into a regexp, such that it will match exactly that text and nothing else.
The string to escape.
The escaped string.
import { escape } from "@std/regexp/escape";import { assertEquals, assertMatch, assertNotMatch } from "@std/assert";const re = new RegExp(`^${escape(".")}$`, "u");assertEquals("^\\.$", re.source);assertMatch(".", re);assertNotMatch("a", re); Copy
import { escape } from "@std/regexp/escape";import { assertEquals, assertMatch, assertNotMatch } from "@std/assert";const re = new RegExp(`^${escape(".")}$`, "u");assertEquals("^\\.$", re.source);assertMatch(".", re);assertNotMatch("a", re);
Escapes arbitrary text for interpolation into a regexp, such that it will match exactly that text and nothing else.