or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-jest-regex-util

Regular expression utilities for Jest providing cross-platform path escaping functions.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/jest-regex-util@30.0.x

To install, run

npx @tessl/cli install tessl/npm-jest-regex-util@30.0.0

index.mddocs/

Jest Regex Util

Jest Regex Util provides essential regular expression utilities for the Jest testing framework, specifically designed to handle cross-platform file path operations. It offers three core functions for escaping paths and strings for use in regular expressions, with special handling for cross-platform path separators.

Package Information

  • Package Name: jest-regex-util
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install jest-regex-util

Core Imports

import { escapePathForRegex, escapeStrForRegex, replacePathSepForRegex } from "jest-regex-util";

For CommonJS:

const { escapePathForRegex, escapeStrForRegex, replacePathSepForRegex } = require("jest-regex-util");

Basic Usage

import { escapePathForRegex, escapeStrForRegex, replacePathSepForRegex } from "jest-regex-util";

// Escape a file path for regex use (cross-platform)
const safePath = escapePathForRegex("/path/to/file.js");
const regex = new RegExp(safePath);

// Escape a string with regex special characters
const safeString = escapeStrForRegex("file(1).js");
// Result: "file\\(1\\)\\.js"

// Convert path separators for regex compatibility
const regexPath = replacePathSepForRegex("/path/with/slashes");
// On Windows: "\\\\path\\\\with\\\\slashes"
// On POSIX: "/path/with/slashes" (unchanged)

Capabilities

Path Escaping

Escapes a complete file path for use in regular expressions with cross-platform path separator handling.

/**
 * Escapes a file path for use in regular expressions with cross-platform handling
 * @param dir - File path to escape
 * @returns Escaped path suitable for regex patterns
 */
function escapePathForRegex(dir: string): string;

This function handles the complete workflow of preparing a file path for regex use:

  1. On Windows, converts backslashes to forward slashes to prevent double-escaping
  2. Escapes all regex special characters using escapeStrForRegex
  3. Converts path separators to platform-appropriate regex patterns using replacePathSepForRegex

String Escaping

Escapes special regex characters in a string to make it literal for use in regular expressions.

/**
 * Escapes special regex characters in a string to make it literal
 * @param string - String to escape
 * @returns String with regex characters escaped
 */
function escapeStrForRegex(string: string): string;

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

Usage Example:

import { escapeStrForRegex } from "jest-regex-util";

const userInput = "user(name).txt";
const escaped = escapeStrForRegex(userInput);
// Result: "user\\(name\\)\\.txt"

// Safe to use in regex
const regex = new RegExp(escaped);

Path Separator Replacement

Converts path separators to be regex-compatible across different platforms (Windows vs POSIX).

/**
 * Converts path separators to be regex-compatible across platforms
 * @param string - String containing path separators
 * @returns String with platform-appropriate escaped separators
 */
function replacePathSepForRegex(string: string): string;

Platform Behavior:

  • POSIX systems: Returns the string unchanged (forward slashes are regex-safe)
  • Windows systems: Converts both forward slashes and backslashes to escaped backslashes (\\\\)

Special handling on Windows:

  • Preserves already-escaped regex characters to avoid double-escaping
  • Handles edge cases like escaped dots (\\.) and other regex symbols
  • Correctly processes path separators within character classes ([/\\])

Usage Example:

import { replacePathSepForRegex } from "jest-regex-util";

// On Windows
const windowsPath = replacePathSepForRegex("src/utils/helper.js");
// Result: "src\\\\utils\\\\helper.js"

// On POSIX
const posixPath = replacePathSepForRegex("src/utils/helper.js");
// Result: "src/utils/helper.js" (unchanged)