/
is-valid-regex.js
63 lines (37 loc) · 1.44 KB
/
is-valid-regex.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module.exports = {
friendlyName: 'Is valid regular expression?',
description: 'Determine whether or not the input string constitutes a valid regular expression.',
extendedDescription: 'Regular expressions should be provided without leading or trailing slashes, and without any modifiers like `m` or `i`.',
sideEffects: 'cacheable',
sync: true,
inputs: {
string: {
example: 'h(e\\w)+llo w[ou]rld',
description: 'The string to test as a regular expression.',
extendedDescription:
'The regular expression should be specified as a string WITHOUT including leading or trailing slashes '+
'or modifiers like `/gi`. For example, don\'t type `/f[o]+/i`. Instead, type `f[o]+`.',
moreInfoUrl: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp',
required: true
}
},
exits: {
success: {
outputFriendlyName: 'Is valid regular expression?',
outputDescription: 'Whether or not the input string is a valid regular expression.',
outputExample: true
},
},
fn: function(inputs, exits) {
// Attempt to make an honest regex out of the string.
try {
new RegExp(inputs.string);
}
// If there were any problems, return `false` through the `success` exit.
catch (e) {
return exits.success(false);
}
// Otherwise return `true` through the `success` exit.
return exits.success(true);
},
};