@jiminp/tooltool
    Preparing search index...

    Function escapeRegExp

    • Escapes special characters in a string for safe use in a regular expression.

      This utility function escapes all characters that have special meaning in regular expressions, allowing you to safely use user input or dynamic strings as literal patterns.

      Parameters

      • s: string

        String to escape.

      Returns string

      The escaped string safe for use in a RegExp constructor.

      This polyfill will be removed in the future. Use native RegExp.escape() when available.

      Escapes the following characters: . * + ? ^ $ { } ( ) | [ ]

      const user_input = 'example.com';
      const pattern = new RegExp(escapeRegExp(user_input));

      pattern.test('example.com'); // true
      pattern.test('exampleXcom'); // false (. is literal, not wildcard)

      // Without escaping, . would match any character
      new RegExp(user_input).test('exampleXcom'); // true (. matches X)