Regular expression utilities for Jest providing cross-platform path escaping functions.
npx @tessl/cli install tessl/npm-jest-regex-util@30.0.0Jest 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.
npm install jest-regex-utilimport { escapePathForRegex, escapeStrForRegex, replacePathSepForRegex } from "jest-regex-util";For CommonJS:
const { escapePathForRegex, escapeStrForRegex, replacePathSepForRegex } = require("jest-regex-util");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)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:
escapeStrForRegexreplacePathSepForRegexEscapes 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);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:
\\\\)Special handling on Windows:
\\.) and other regex symbols[/\\])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)