{"id":13972,"library":"safe-regex-test","title":"Safe Regex Tester","description":"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.","status":"active","version":"1.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/ljharb/safe-regex-test","tags":["javascript","regex","regexp","test","tester","safe","robust","exec","typescript"],"install":[{"cmd":"npm install safe-regex-test","lang":"bash","label":"npm"},{"cmd":"yarn add safe-regex-test","lang":"bash","label":"yarn"},{"cmd":"pnpm add safe-regex-test","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Internal utility for robust function binding.","package":"call-bound","optional":false},{"reason":"Provides robust error constructors for various JavaScript error types.","package":"es-errors","optional":false},{"reason":"Determines if a value is a regular expression object.","package":"is-regex","optional":false}],"imports":[{"note":"The package exports a default function for ESM. Do not use named imports.","wrong":"import { safeRegexTest } from 'safe-regex-test';","symbol":"safeRegexTest","correct":"import safeRegexTest from 'safe-regex-test';"},{"note":"CommonJS imports the default function directly.","wrong":"const { safeRegexTest } = require('safe-regex-test');","symbol":"safeRegexTest","correct":"const safeRegexTest = require('safe-regex-test');"},{"note":"Type definition for the main exported function, useful for strict TypeScript usage.","symbol":"SafeRegexTester","correct":"import type { SafeRegexTester } from 'safe-regex-test';"}],"quickstart":{"code":"import safeRegexTest from 'safe-regex-test';\nimport assert from 'assert';\n\n// Create a robust regex tester function\nconst tester = safeRegexTest(/hello/i);\n\n// Test strings\nassert.ok(tester('Hello, world!'));\nassert.notOk(tester('Goodbye, world!'));\n\n// Demonstrate robustness against prototype pollution (conceptual, not runnable here without polluting global)\n// This library ensures that even if RegExp.prototype.test were modified, 'tester' would still behave correctly.\n// For example, if someone did: Object.defineProperty(RegExp.prototype, 'test', { value: () => true });\n// The 'tester' function created by safe-regex-test would still use the original, unpolluted 'test' logic.\n\nconst alwaysTrueTester = safeRegexTest(/any/);\nconsole.log('Testing with original regex:', alwaysTrueTester('any string'));\n","lang":"typescript","description":"Demonstrates how to import and use `safe-regex-test` to create a robust regex testing function, highlighting its core capability."},"warnings":[{"fix":"For ReDoS protection, consider using a dedicated library like 'safe-regex' (a different package) or 'recheck'.","message":"The '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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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+'`).","message":"Directly 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Refactor imports from `const safeRegexTest = require('safe-regex-test');` to `import safeRegexTest from 'safe-regex-test';`","message":"While CommonJS `require()` is still supported, new projects are encouraged to use ES Modules (`import`).","severity":"deprecated","affected_versions":"<=1.1.0"}],"env_vars":null,"search_vec":"'1.1.0':65 'addit':95 'address':27 'ago':71 'alter':40 'altern':109 'behavior':53 'bug':89 'cadenc':82 'concern':31 'context':121 'control':58 'current':61 'deliber':80 'design':12 'differenti':98 'direct':111 'environ':59 'exec':129 'execut':120 'express':23 'featur':94 'fix':90 'focus':84 'frequent':93 'global':45 'indic':72 'javascript':10,122 'key':97 'last':66 'lead':50 'less':57 'matur':74 'mechan':37 'method':19 'object':47 'offer':105 'particular':114 'pollut':42 'potenti':39,118 'predict':18 'primarili':83 'project':75 'prototyp':103 'provid':14 'publish':67 'rather':91 'regex':2,6,35,123 'regexp':124 'regexp.prototype':46 'regexp.prototype.test':112 'regular':22 'releas':81 'resili':101 'robust':16,128 'safe':1,5,127 'safe-regex-test':4 'safeguard':33 'secur':30,108 'share':116 'slow':78 'specif':29,88 'stabil':86 'stabl':62 'string':25 'tamper':104 'test':7,21,36,125 'tester':3,126 'typescript':130 'unexpect':52 'untrust':119 'usag':113 'util':11 'version':63 'vulner':55 'year':70","created_at":"2026-04-20T01:57:16.390312+00:00","updated_at":"2026-04-20T01:57:16.390312+00:00","problems":[{"fix":"Change `import { safeRegexTest } from 'safe-regex-test';` to `import safeRegexTest from 'safe-regex-test';`","cause":"Incorrect ESM named import syntax for a default export.","error":"TypeError: (0, _safeRegexTest.default) is not a function"},{"fix":"Ensure the input string passed to `safeRegexTest` represents a syntactically valid regular expression, e.g., `/^abc$/` or `'abc'`.","cause":"Attempting to create a regex tester with an invalid regular expression string.","error":"Error: Invalid regular expression: /(a+/): Unterminated character class"},{"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.","cause":"Attempting to call the returned tester function with `null` or `undefined` as a string, or binding it incorrectly.","error":"TypeError: Cannot read properties of undefined (reading 'test')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/ljharb/safe-regex-test","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/safe-regex-test","openapi_spec":null,"status_page":null,"smithery":null,"categories":["auth-security","testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}