Negative Regular Expression Generator
regex-not, currently at version 1.0.2, is a utility package designed to simplify the creation of JavaScript regular expressions that match everything *except* a given string. It abstracts away the complexity of crafting negative lookahead assertions, which can be challenging and error-prone to write manually. The library is primarily a CommonJS module, targeted at Node.js environments from version 0.10.0 and above. Its release cadence is infrequent, suggesting a mature, stable API that is in a maintenance phase rather than active feature development. A key differentiator is its dual functionality: it can generate regexes for strict negation (the string does *not* exactly match) or 'contains' negation (the string does *not* contain the substring), providing flexibility for various inverse matching requirements.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ES Modules (ESM) context without a transpiler or specific Node.js configuration.fixIf your project is ESM-first, try `import not from 'regex-not';` (though this package is CJS-primary, Node.js often handles this for default exports). Alternatively, convert your file to CommonJS if possible, or configure a bundler (like Webpack or Rollup) to handle CJS modules in an ESM project. -
My regex doesn't work as expected; it matches strings that contain the pattern I want to exclude!
cause Misunderstanding the default 'strict' matching behavior of `regex-not`. By default, it excludes only exact matches, not partial containment.fixIf you want the regex to match strings that *do not contain* a specific substring, you need to use the `contains` option: `not('your_pattern', { contains: true })`.
Warnings
- gotcha The package is primarily designed for CommonJS (CJS) environments and uses `require()`. Direct `import` statements in ES Modules (ESM) will likely fail without a bundler or specific Node.js configuration for CJS interoperability.
- gotcha By default, `regex-not` generates a regex for *strict* negation, meaning it matches if the input string does NOT *exactly equal* the pattern. If you intend to match strings that do NOT *contain* a substring, you must explicitly set `options.contains: true`.
- gotcha The package's low version (1.0.2) and infrequent updates indicate it's in maintenance mode. While stable for its current functionality, it may not receive updates for new JavaScript features, performance optimizations, or modern API patterns.
Install
-
npm install regex-not -
yarn add regex-not -
pnpm add regex-not
Imports
- not
import not from 'regex-not';
const not = require('regex-not'); - not.create
import { create } from 'regex-not';const not = require('regex-not'); const regexString = not.create('foo');
Quickstart
const not = require('regex-not');
// Default behavior: matches if the string does NOT exactly equal 'foo'
const reStrict = not('foo');
console.log('Strict matching:');
console.log(`'foo' should be false: ${reStrict.test('foo')}`); // Expected: false
console.log(`'bar' should be true: ${reStrict.test('bar')}`); // Expected: true
console.log(`'foobar' should be true: ${reStrict.test('foobar')}`); // Expected: true (not exact)
// Option: matches if the string does NOT contain 'foo'
const reContains = not('foo', { contains: true });
console.log('\nContains matching:');
console.log(`'foo' should be false: ${reContains.test('foo')}`); // Expected: false
console.log(`'bar' should be true: ${reContains.test('bar')}`); // Expected: true
console.log(`'foobar' should be false: ${reContains.test('foobar')}`); // Expected: false (contains 'foo')
// Get just the regex pattern string
const patternString = not.create('bar');
console.log(`\nGenerated pattern string for 'bar': ${patternString}`);
const customRegExp = new RegExp(patternString);
console.log(`Using custom RegExp: '${customRegExp.source}'.test('bar') should be false: ${customRegExp.test('bar')}`);