Function escape

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

    Parameters

    • str: string

      The string to escape.

    Returns string

    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);