├── .editorconfig
├── .eslintrc.json
├── .gitattributes
├── .gitignore
├── .travis.yml
├── .verb.md
├── LICENSE
├── README.md
├── index.js
├── package.json
└── test.js
/.editorconfig:
--------------------------------------------------------------------------------
1 | # http://editorconfig.org/
2 | root = true
3 |
4 | [*]
5 | charset = utf-8
6 | end_of_line = lf
7 | indent_size = 2
8 | indent_style = space
9 | insert_final_newline = true
10 | trim_trailing_whitespace = true
11 |
12 | [{**/{actual,fixtures,expected,templates}/**,*.md}]
13 | trim_trailing_whitespace = false
14 | insert_final_newline = false
15 |
--------------------------------------------------------------------------------
/.eslintrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": [
3 | "eslint:recommended"
4 | ],
5 |
6 | "env": {
7 | "browser": false,
8 | "es6": true,
9 | "node": true,
10 | "mocha": true
11 | },
12 |
13 | "parserOptions":{
14 | "ecmaVersion": 9,
15 | "sourceType": "module",
16 | "ecmaFeatures": {
17 | "modules": true,
18 | "experimentalObjectRestSpread": true
19 | }
20 | },
21 |
22 | "globals": {
23 | "document": false,
24 | "navigator": false,
25 | "window": false
26 | },
27 |
28 | "rules": {
29 | "accessor-pairs": 2,
30 | "arrow-spacing": [2, { "before": true, "after": true }],
31 | "block-spacing": [2, "always"],
32 | "brace-style": [2, "1tbs", { "allowSingleLine": true }],
33 | "comma-dangle": [2, "never"],
34 | "comma-spacing": [2, { "before": false, "after": true }],
35 | "comma-style": [2, "last"],
36 | "constructor-super": 2,
37 | "curly": [2, "multi-line"],
38 | "dot-location": [2, "property"],
39 | "eol-last": 2,
40 | "eqeqeq": [2, "allow-null"],
41 | "generator-star-spacing": [2, { "before": true, "after": true }],
42 | "handle-callback-err": [2, "^(err|error)$" ],
43 | "indent": [2, 2, { "SwitchCase": 1 }],
44 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }],
45 | "keyword-spacing": [2, { "before": true, "after": true }],
46 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }],
47 | "new-parens": 2,
48 | "no-array-constructor": 2,
49 | "no-caller": 2,
50 | "no-class-assign": 2,
51 | "no-cond-assign": 2,
52 | "no-const-assign": 2,
53 | "no-control-regex": 2,
54 | "no-debugger": 2,
55 | "no-delete-var": 2,
56 | "no-dupe-args": 2,
57 | "no-dupe-class-members": 2,
58 | "no-dupe-keys": 2,
59 | "no-duplicate-case": 2,
60 | "no-empty-character-class": 2,
61 | "no-eval": 2,
62 | "no-ex-assign": 2,
63 | "no-extend-native": 2,
64 | "no-extra-bind": 2,
65 | "no-extra-boolean-cast": 2,
66 | "no-extra-parens": [2, "functions"],
67 | "no-fallthrough": 2,
68 | "no-floating-decimal": 2,
69 | "no-func-assign": 2,
70 | "no-implied-eval": 2,
71 | "no-inner-declarations": [2, "functions"],
72 | "no-invalid-regexp": 2,
73 | "no-irregular-whitespace": 2,
74 | "no-iterator": 2,
75 | "no-label-var": 2,
76 | "no-labels": 2,
77 | "no-lone-blocks": 2,
78 | "no-mixed-spaces-and-tabs": 2,
79 | "no-multi-spaces": 2,
80 | "no-multi-str": 2,
81 | "no-multiple-empty-lines": [2, { "max": 1 }],
82 | "no-native-reassign": 0,
83 | "no-negated-in-lhs": 2,
84 | "no-new": 2,
85 | "no-new-func": 2,
86 | "no-new-object": 2,
87 | "no-new-require": 2,
88 | "no-new-wrappers": 2,
89 | "no-obj-calls": 2,
90 | "no-octal": 2,
91 | "no-octal-escape": 2,
92 | "no-proto": 0,
93 | "no-redeclare": 2,
94 | "no-regex-spaces": 2,
95 | "no-return-assign": 2,
96 | "no-self-compare": 2,
97 | "no-sequences": 2,
98 | "no-shadow-restricted-names": 2,
99 | "no-spaced-func": 2,
100 | "no-sparse-arrays": 2,
101 | "no-this-before-super": 2,
102 | "no-throw-literal": 2,
103 | "no-trailing-spaces": 0,
104 | "no-undef": 2,
105 | "no-undef-init": 2,
106 | "no-unexpected-multiline": 2,
107 | "no-unneeded-ternary": [2, { "defaultAssignment": false }],
108 | "no-unreachable": 2,
109 | "no-unused-vars": [2, { "vars": "all", "args": "none" }],
110 | "no-useless-call": 0,
111 | "no-with": 2,
112 | "one-var": [0, { "initialized": "never" }],
113 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }],
114 | "padded-blocks": [0, "never"],
115 | "quotes": [2, "single", "avoid-escape"],
116 | "radix": 2,
117 | "semi": [2, "always"],
118 | "semi-spacing": [2, { "before": false, "after": true }],
119 | "space-before-blocks": [2, "always"],
120 | "space-before-function-paren": [2, "never"],
121 | "space-in-parens": [2, "never"],
122 | "space-infix-ops": 2,
123 | "space-unary-ops": [2, { "words": true, "nonwords": false }],
124 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }],
125 | "use-isnan": 2,
126 | "valid-typeof": 2,
127 | "wrap-iife": [2, "any"],
128 | "yoda": [2, "never"]
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Enforce Unix newlines
2 | * text eol=lf
3 |
4 | # binaries
5 | *.ai binary
6 | *.psd binary
7 | *.jpg binary
8 | *.gif binary
9 | *.png binary
10 | *.jpeg binary
11 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # always ignore files
2 | *.DS_Store
3 | .idea
4 | .vscode
5 | *.sublime-*
6 |
7 | # test related, or directories generated by tests
8 | test/actual
9 | actual
10 | coverage
11 | .nyc*
12 |
13 | # npm
14 | node_modules
15 | npm-debug.log
16 |
17 | # yarn
18 | yarn.lock
19 | yarn-error.log
20 |
21 | # misc
22 | _gh_pages
23 | _draft
24 | _drafts
25 | bower_components
26 | vendor
27 | temp
28 | tmp
29 | TODO.md
30 | package-lock.json
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: false
2 | os:
3 | - linux
4 | - osx
5 | language: node_js
6 | node_js:
7 | - node
8 | - '9'
9 | - '8'
10 | - '7'
11 | - '6'
12 | - '5'
13 | - '4'
14 | - '0.12'
15 | - '0.10'
16 |
--------------------------------------------------------------------------------
/.verb.md:
--------------------------------------------------------------------------------
1 | ## Usage
2 |
3 | ```js
4 | var not = require('regex-not');
5 | ```
6 |
7 | The main export is a function that takes a string an options object.
8 |
9 | ```js
10 | not(string[, options]);
11 | ```
12 |
13 | **Example**
14 |
15 | ```js
16 | var not = require('regex-not');
17 | console.log(not('foo'));
18 | //=> /^(?:(?!^(?:foo)$).)+$/
19 | ```
20 |
21 | **Strict matching**
22 |
23 | By default, the returned regex is for strictly (not) matching the exact given pattern (in other words, "match this string if it does NOT _exactly equal_ `foo`"):
24 |
25 | ```js
26 | var re = not('foo');
27 | console.log(re.test('foo')); //=> false
28 | console.log(re.test('bar')); //=> true
29 | console.log(re.test('foobar')); //=> true
30 | console.log(re.test('barfoo')); //=> true
31 | ```
32 |
33 | ### .create
34 |
35 | Returns a string to allow you to create your own regex:
36 |
37 | ```js
38 | console.log(not.create('foo'));
39 | //=> '(?:(?!^(?:foo)$).)+'
40 | ```
41 |
42 | ### Options
43 |
44 | **options.contains**
45 |
46 | You can relax strict matching by setting `options.contains` to true (in other words, "match this string if it does NOT _contain_ `foo`"):
47 |
48 | ```js
49 | var re = not('foo');
50 | console.log(re.test('foo', {contains: true})); //=> false
51 | console.log(re.test('bar', {contains: true})); //=> true
52 | console.log(re.test('foobar', {contains: true})); //=> false
53 | console.log(re.test('barfoo', {contains: true})); //=> false
54 | ```
55 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016, 2018, Jon Schlinkert.
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # regex-not [](https://www.npmjs.com/package/regex-not) [](https://npmjs.org/package/regex-not) [](https://npmjs.org/package/regex-not) [](https://travis-ci.org/jonschlinkert/regex-not)
2 |
3 | > Create a javascript regular expression for matching everything except for the given string.
4 |
5 | Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
6 |
7 | ## Install
8 |
9 | Install with [npm](https://www.npmjs.com/):
10 |
11 | ```sh
12 | $ npm install --save regex-not
13 | ```
14 |
15 | ## Usage
16 |
17 | ```js
18 | var not = require('regex-not');
19 | ```
20 |
21 | The main export is a function that takes a string an options object.
22 |
23 | ```js
24 | not(string[, options]);
25 | ```
26 |
27 | **Example**
28 |
29 | ```js
30 | var not = require('regex-not');
31 | console.log(not('foo'));
32 | //=> /^(?:(?!^(?:foo)$).)+$/
33 | ```
34 |
35 | **Strict matching**
36 |
37 | By default, the returned regex is for strictly (not) matching the exact given pattern (in other words, "match this string if it does NOT _exactly equal_ `foo`"):
38 |
39 | ```js
40 | var re = not('foo');
41 | console.log(re.test('foo')); //=> false
42 | console.log(re.test('bar')); //=> true
43 | console.log(re.test('foobar')); //=> true
44 | console.log(re.test('barfoo')); //=> true
45 | ```
46 |
47 | ### .create
48 |
49 | Returns a string to allow you to create your own regex:
50 |
51 | ```js
52 | console.log(not.create('foo'));
53 | //=> '(?:(?!^(?:foo)$).)+'
54 | ```
55 |
56 | ### Options
57 |
58 | **options.contains**
59 |
60 | You can relax strict matching by setting `options.contains` to true (in other words, "match this string if it does NOT _contain_ `foo`"):
61 |
62 | ```js
63 | var re = not('foo');
64 | console.log(re.test('foo', {contains: true})); //=> false
65 | console.log(re.test('bar', {contains: true})); //=> true
66 | console.log(re.test('foobar', {contains: true})); //=> false
67 | console.log(re.test('barfoo', {contains: true})); //=> false
68 | ```
69 |
70 | ## About
71 |
72 |
73 | Contributing
74 |
75 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
76 |
77 |
78 |
79 |
80 | Running Tests
81 |
82 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
83 |
84 | ```sh
85 | $ npm install && npm test
86 | ```
87 |
88 |
89 |
90 |
91 | Building docs
92 |
93 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
94 |
95 | To generate the readme, run the following command:
96 |
97 | ```sh
98 | $ npm install -g verbose/verb#dev verb-generate-readme && verb
99 | ```
100 |
101 |
102 |
103 | ### Related projects
104 |
105 | You might also be interested in these projects:
106 |
107 | * [regex-cache](https://www.npmjs.com/package/regex-cache): Memoize the results of a call to the RegExp constructor, avoiding repetitious runtime compilation of… [more](https://github.com/jonschlinkert/regex-cache) | [homepage](https://github.com/jonschlinkert/regex-cache "Memoize the results of a call to the RegExp constructor, avoiding repetitious runtime compilation of the same string and options, resulting in surprising performance improvements.")
108 | * [to-regex](https://www.npmjs.com/package/to-regex): Generate a regex from a string or array of strings. | [homepage](https://github.com/jonschlinkert/to-regex "Generate a regex from a string or array of strings.")
109 |
110 | ### Contributors
111 |
112 | | **Commits** | **Contributor** |
113 | | --- | --- |
114 | | 9 | [jonschlinkert](https://github.com/jonschlinkert) |
115 | | 1 | [doowb](https://github.com/doowb) |
116 | | 1 | [EdwardBetts](https://github.com/EdwardBetts) |
117 |
118 | ### Author
119 |
120 | **Jon Schlinkert**
121 |
122 | * [linkedin/in/jonschlinkert](https://linkedin.com/in/jonschlinkert)
123 | * [github/jonschlinkert](https://github.com/jonschlinkert)
124 | * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
125 |
126 | ### License
127 |
128 | Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
129 | Released under the [MIT License](LICENSE).
130 |
131 | ***
132 |
133 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on February 19, 2018._
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var extend = require('extend-shallow');
4 | var safe = require('safe-regex');
5 |
6 | /**
7 | * The main export is a function that takes a `pattern` string and an `options` object.
8 | *
9 | * ```js
10 | & var not = require('regex-not');
11 | & console.log(not('foo'));
12 | & //=> /^(?:(?!^(?:foo)$).)*$/
13 | * ```
14 | *
15 | * @param {String} `pattern`
16 | * @param {Object} `options`
17 | * @return {RegExp} Converts the given `pattern` to a regex using the specified `options`.
18 | * @api public
19 | */
20 |
21 | function toRegex(pattern, options) {
22 | return new RegExp(toRegex.create(pattern, options));
23 | }
24 |
25 | /**
26 | * Create a regex-compatible string from the given `pattern` and `options`.
27 | *
28 | * ```js
29 | & var not = require('regex-not');
30 | & console.log(not.create('foo'));
31 | & //=> '^(?:(?!^(?:foo)$).)*$'
32 | * ```
33 | * @param {String} `pattern`
34 | * @param {Object} `options`
35 | * @return {String}
36 | * @api public
37 | */
38 |
39 | toRegex.create = function(pattern, options) {
40 | if (typeof pattern !== 'string') {
41 | throw new TypeError('expected a string');
42 | }
43 |
44 | var opts = extend({}, options);
45 | if (opts.contains === true) {
46 | opts.strictNegate = false;
47 | }
48 |
49 | var open = opts.strictOpen !== false ? '^' : '';
50 | var close = opts.strictClose !== false ? '$' : '';
51 | var endChar = opts.endChar ? opts.endChar : '+';
52 | var str = pattern;
53 |
54 | if (opts.strictNegate === false) {
55 | str = '(?:(?!(?:' + pattern + ')).)' + endChar;
56 | } else {
57 | str = '(?:(?!^(?:' + pattern + ')$).)' + endChar;
58 | }
59 |
60 | var res = open + str + close;
61 | if (opts.safe === true && safe(res) === false) {
62 | throw new Error('potentially unsafe regular expression: ' + res);
63 | }
64 |
65 | return res;
66 | };
67 |
68 | /**
69 | * Expose `toRegex`
70 | */
71 |
72 | module.exports = toRegex;
73 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "regex-not",
3 | "description": "Create a javascript regular expression for matching everything except for the given string.",
4 | "version": "1.0.2",
5 | "homepage": "https://github.com/jonschlinkert/regex-not",
6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)",
7 | "repository": "jonschlinkert/regex-not",
8 | "bugs": {
9 | "url": "https://github.com/jonschlinkert/regex-not/issues"
10 | },
11 | "license": "MIT",
12 | "files": [
13 | "index.js"
14 | ],
15 | "main": "index.js",
16 | "engines": {
17 | "node": ">=0.10.0"
18 | },
19 | "scripts": {
20 | "test": "mocha"
21 | },
22 | "dependencies": {
23 | "extend-shallow": "^3.0.2",
24 | "safe-regex": "^1.1.0"
25 | },
26 | "devDependencies": {
27 | "gulp-format-md": "^1.0.0",
28 | "mocha": "^3.5.3"
29 | },
30 | "keywords": [
31 | "exec",
32 | "match",
33 | "negate",
34 | "negation",
35 | "not",
36 | "regex",
37 | "regular expression",
38 | "test"
39 | ],
40 | "verb": {
41 | "toc": false,
42 | "layout": "default",
43 | "tasks": [
44 | "readme"
45 | ],
46 | "plugins": [
47 | "gulp-format-md"
48 | ],
49 | "related": {
50 | "list": [
51 | "regex-cache",
52 | "to-regex"
53 | ]
54 | },
55 | "reflinks": [
56 | "verb",
57 | "verb-generate-readme"
58 | ],
59 | "lint": {
60 | "reflinks": true
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/test.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | require('mocha');
4 | var assert = require('assert');
5 | var not = require('./');
6 |
7 | describe('regex-not', function() {
8 | it('should export a function', function() {
9 | assert.equal(typeof not, 'function');
10 | });
11 |
12 | it('should create a negation regex', function() {
13 | var re = not('foo');
14 | assert.deepEqual(re, /^(?:(?!^(?:foo)$).)+$/);
15 | assert.equal(re.test('foo'), false);
16 | assert.equal(re.test('bar'), true);
17 | assert.equal(re.test('foobar'), true);
18 | assert.equal(re.test('barfoo'), true);
19 | });
20 |
21 | it('should create a loose negation regex when `options.contains` is true', function() {
22 | assert.deepEqual(not('foo', {contains: true}), /^(?:(?!(?:foo)).)+$/);
23 | assert.equal(not('foo', {contains: true}).test('foo'), false);
24 | assert.equal(not('foo', {contains: true}).test('bar'), true);
25 | assert.equal(not('foo', {contains: true}).test('foobar'), false);
26 | assert.equal(not('foo', {contains: true}).test('barfoo'), false);
27 | });
28 |
29 | it('should create a loose negation regex when `options.strictNegate` is false', function() {
30 | var opts = {strictNegate: false};
31 | assert.deepEqual(not('foo', opts), /^(?:(?!(?:foo)).)+$/);
32 | assert.equal(not('foo', opts).test('foo'), false);
33 | assert.equal(not('foo', opts).test('bar'), true);
34 | assert.equal(not('foo', opts).test('foobar'), false);
35 | assert.equal(not('foo', opts).test('barfoo'), false);
36 | });
37 |
38 | it('should support `options.endChar`', function() {
39 | var opts = {endChar: '*'};
40 | assert.deepEqual(not('foo', opts), /^(?:(?!^(?:foo)$).)*$/);
41 | assert.deepEqual(not('foo', opts).exec('foo'), null);
42 | assert.equal(not('foo', opts).test('foo'), false);
43 | assert.equal(not('foo', opts).test('bar'), true);
44 | assert.equal(not('foo', opts).test('foobar'), true);
45 | assert.equal(not('foo', opts).test('barfoo'), true);
46 | });
47 |
48 | it('should throw when a potentially unsafe regex is passed', function() {
49 | assert.throws(function() {
50 | not('(x+x+)+y', { safe: true });
51 | }, /potentially unsafe/);
52 | });
53 |
54 | it('should throw an error when invalid args are passed', function() {
55 | assert.throws(function() {
56 | not();
57 | }, /expected/);
58 | });
59 | });
60 |
--------------------------------------------------------------------------------