diff --git a/regexp/escape.ts b/regexp/escape.ts index 7be38846f714..da1a8bc98476 100644 --- a/regexp/escape.ts +++ b/regexp/escape.ts @@ -1,6 +1,24 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // This module is browser compatible. +/** + * This module contains functions to escape strings for use in regular expressions. + * + * @example + * ```ts + * 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); + * ``` + * + * @module + */ + // // For future forward-compatibility with regexp `v` flag, reservedCharMap is // // autogenerated from the ClassSetReservedDoublePunctuator, // // ClassSetSyntaxCharacter, and ClassSetReservedPunctuator categories in the @@ -27,7 +45,7 @@ const reservedCharMap = { "&": "\\x26", "!": "\\x21", "#": "\\x23", - "$": "\\$", + $: "\\$", "%": "\\x25", "*": "\\*", "+": "\\+", diff --git a/regexp/mod.ts b/regexp/mod.ts index cab2ada81b5b..fe5369c307fe 100644 --- a/regexp/mod.ts +++ b/regexp/mod.ts @@ -7,6 +7,18 @@ * such as escaping text for interpolation into a regexp. * * @module + * + * @example + * ```ts + * import { escape } from "@std/regexp"; + * import { assertEquals, assertMatch, assertNotMatch } from "@std/assert"; + * + * const re = new RegExp(`^${escape(".")}$`, "u"); + * + * assertEquals("^\\.$", re.source); + * assertMatch(".", re); + * assertNotMatch("a", re); + * ``` */ export * from "./escape.ts";