Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

std/regexp/mod.ts>escape

Deno standard library
Go to Latest
The Standard Library has been moved to JSR. See the blog post for details.
function escape
import { escape } from "https://deno.land/std@0.193.0/regexp/mod.ts";

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

Examples

Example 1

import { escape } from "https://deno.land/std@0.193.0/regexp/mod.ts";
import { assertEquals, assertMatch, assertNotMatch } from "https://deno.land/std@0.193.0/testing/asserts.ts";

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

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

Parameters

str: string