UNPKG

3.26 kBTypeScriptView Raw
1export = regexNot;
2
3/**
4 * Create a javascript regular expression for matching everything except for the given string.
5 *
6 * ### Strict matching ###
7 * By default, the returned regex is for strictly (not) matching the exact given pattern
8 * (in other words, "match this string if it does NOT exactly equal `foo`").
9 *
10 * @example
11 * import not = require('regex-not');
12 *
13 * console.log(not('foo')); //=> /^(?:(?!^(?:foo)$).)+$/
14 * console.log(re.test('foo')); //=> false
15 * console.log(re.test('bar')); //=> true
16 * console.log(re.test('foobar')); //=> true
17 * console.log(re.test('barfoo')); //=> true
18 */
19declare function regexNot(str: string, options?: regexNot.Options): RegExp;
20
21declare namespace regexNot {
22 /**
23 * @returns A string to allow you to create your own regex.
24 *
25 * @example
26 * import not = require('regex-not');
27 *
28 * console.log(not.create('foo'));
29 * //=> '^(?:(?!^(?:foo)$).)+$'
30 */
31 function create(str: string, options?: Options): string;
32
33 interface Options {
34 /**
35 * You can relax strict matching by setting `contains` to `true` (in other words,
36 * "match this string if it does NOT contain `foo`").
37 *
38 * @default false
39 *
40 * @example
41 * import not = require('regex-not');
42 *
43 * const re = not('foo', {contains: true});
44 * console.log(re.test('foo')); //=> false
45 * console.log(re.test('bar')); //=> true
46 * console.log(re.test('foobar')); //=> false
47 * console.log(re.test('barfoo')); //=> false
48 */
49 contains?: boolean;
50
51 /**
52 * Controls whether the start of input anchor (`^`) should be generated for the pattern.
53 *
54 * @default true
55 *
56 * @example
57 * import not = require('regex-not');
58 *
59 * const re = not('foo'); // => '^(?:(?!^(?:foo)$).)+$'
60 * const re = not('foo', {strictOpen: '*'}); // => '(?:(?!^(?:foo)$).)+$'
61 */
62 strictOpen?: boolean;
63
64 /**
65 * Controls whether the end of input anchor (`$`) should be generated for the pattern.
66 *
67 * @default true
68 *
69 * @example
70 * import not = require('regex-not');
71 *
72 * const re = not('foo'); // => '^(?:(?!^(?:foo)$).)+$'
73 * const re = not('foo', {strictClose: '*'}); // => '^(?:(?!^(?:foo)$).)+'
74 */
75 strictClose?: boolean;
76
77 /**
78 * Controls the outermost repetition modifier in the generated RegExp.
79 *
80 * @default '+'
81 *
82 * @example
83 * import not = require('regex-not');
84 *
85 * const re = not('foo'); // => '^(?:(?!^(?:foo)$).)+$'
86 * const re = not('foo', {endChar: '*'}); // => '^(?:(?!^(?:foo)$).)*$'
87 */
88 endChar?: string;
89
90 /**
91 * Throw an error when a potentially catastrophic exponential-time regular expression is detected.
92 * See [strict-regex](https://github.com/davisjam/safe-regex) for more details and potential implications.
93 *
94 * @default false
95 */
96 safe?: boolean;
97 }
98}