Negative Regular Expression Generator

1.0.2 · maintenance · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Demonstrates strict (default) and 'contains' negation using the `not` function, and how to get the raw regex pattern string using `not.create`.

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')}`);

view raw JSON →