Registry / auth-security / safe-regex-test

safe-regex-test

JSON →
library1.1.0jsnpmunverified

safe-regex-test is a JavaScript utility designed to provide a robust and predictable method for testing regular expressions against strings. It addresses a specific security concern by safeguarding the regex testing mechanism from potential alterations or pollution of the global `RegExp.prototype` object, which can lead to unexpected behavior or vulnerabilities in less controlled environments. The current stable version is 1.1.0, last published over a year ago, indicating a mature project with a slow and deliberate release cadence, primarily focused on stability and specific bug fixes rather than frequent feature additions. Its key differentiator is this resilience against prototype tampering, offering a more secure alternative to direct `RegExp.prototype.test` usage, particularly in shared or potentially untrusted execution contexts.

npm install safe-regex-test
INSTALL
IMPORT
SIG · SAFE-REGEX-TEST
S
safe-regex-test
auth-securityjavascriptv1.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

safeRegexTest
import safeRegexTest from 'safe-regex-test';
import { safeRegexTest } from 'safe-regex-test';
The package exports a default function for ESM. Do not use named imports.
safeRegexTest
const safeRegexTest = require('safe-regex-test');
const { safeRegexTest } = require('safe-regex-test');
CommonJS imports the default function directly.
SafeRegexTester
import type { SafeRegexTester } from 'safe-regex-test';
Type definition for the main exported function, useful for strict TypeScript usage.

Demonstrates how to import and use `safe-regex-test` to create a robust regex testing function, highlighting its core capability.

import safeRegexTest from 'safe-regex-test'; import assert from 'assert'; // Create a robust regex tester function const tester = safeRegexTest(/hello/i); // Test strings assert.ok(tester('Hello, world!')); assert.notOk(tester('Goodbye, world!')); // Demonstrate robustness against prototype pollution (conceptual, not runnable here without polluting global) // This library ensures that even if RegExp.prototype.test were modified, 'tester' would still behave correctly. // For example, if someone did: Object.defineProperty(RegExp.prototype, 'test', { value: () => true }); // The 'tester' function created by safe-regex-test would still use the original, unpolluted 'test' logic. const alwaysTrueTester = safeRegexTest(/any/); console.log('Testing with original regex:', alwaysTrueTester('any string'));
Debug
Known issues
gotchaThe 'safe' in 'safe-regex-test' refers to robustness against `RegExp.prototype` alterations, NOT protection against ReDoS (Regular expression Denial of Service) vulnerabilities. This package does not analyze regex patterns for catastrophic backtracking or exponential time complexity.
fix
For ReDoS protection, consider using a dedicated library like 'safe-regex' (a different package) or 'recheck'.
affects: >=1.0.0
gotchaDirectly passing a non-RegExp object or an invalid regex string to `safeRegexTest` will result in an error or unexpected behavior, as the underlying `RegExp` constructor will be invoked.
fix
Always ensure the input to `safeRegexTest` is a valid `RegExp` instance or a string that can be safely converted to one (e.g., `'a'` not `'(a+'`).
affects: >=1.0.0
deprecatedWhile CommonJS `require()` is still supported, new projects are encouraged to use ES Modules (`import`).
fix
Refactor imports from `const safeRegexTest = require('safe-regex-test');` to `import safeRegexTest from 'safe-regex-test';`
affects: <=1.1.0
Errors
Common errors & fixes
TypeError: (0, _safeRegexTest.default) is not a function
Incorrect ESM named import syntax for a default export.
fix
Change `import { safeRegexTest } from 'safe-regex-test';` to `import safeRegexTest from 'safe-regex-test';`
Error: Invalid regular expression: /(a+/): Unterminated character class
Attempting to create a regex tester with an invalid regular expression string.
fix
Ensure the input string passed to `safeRegexTest` represents a syntactically valid regular expression, e.g., `/^abc$/` or `'abc'`.
TypeError: Cannot read properties of undefined (reading 'test')
Attempting to call the returned tester function with `null` or `undefined` as a string, or binding it incorrectly.
fix
Ensure the string passed to the tester function is always a string. If using `bind`, ensure `this` context is appropriate or use an arrow function.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies
call-boundrequiredInternal utility for robust function binding.
es-errorsrequiredProvides robust error constructors for various JavaScript error types.
is-regexrequiredDetermines if a value is a regular expression object.
Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
1
Resources
safe-regex-test — npm install safe-regex-test · libregistry