forked from 0xSobky/HackVault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regaxor.js
126 lines (111 loc) · 3.05 KB
/
regaxor.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* A wrapper object for helper functions.
*/
let regaxorHelpers = {
padLeft(str) {
return str[0] + str;
},
padRight(str) {
return str + str[str.length - 1];
},
replaceDots(str) {
return str.replace(/\./g, 'X');
},
doubleEscape(str) {
return str.replace(/\\/g, '\\\\');
},
prefixLn(str) {
return '\r\n\n\r' + str;
},
postfixLn(str) {
return str + '\r\n\n\r';
},
replicateLn(str) {
return str.repeat(2) + '\u2028' + str;
},
sliceRight(str) {
let length = str.length / 2;
return str.slice(length);
},
sliceLeft(str) {
let length = str.length / 2;
return str.slice(0, length);
},
flip(str) {
let length = str.length / 2;
return str.slice(length) + str.slice(0, length);
},
reverse(str) {
str = str.split('');
str = str.reverse();
return str.join('');
},
changeCase(str) {
if (str.toLowerCase() === str)
str = str.toUpperCase();
else
str = str.toLowerCase();
return str;
},
shuffle(str) {
let arr = str.split('');
let index = arr.length;
while (index--) {
let rand = Math.floor(Math.random() * (index + 1));
[arr[rand], arr[index]] = [arr[index], arr[rand]];
}
return arr.join('');
},
shift(str) {
let arr = str.split('');
let index = arr.length;
let rand = Math.ceil(Math.random() * 5);
while (index--) {
let char = arr[index];
arr[index] = String.fromCharCode(
char.charCodeAt(char) << rand
);
}
return arr.join('');
}
};
/**
* Fuzz a given regular expression.
* @param {string} str - A raw string.
* @param {object|string} re - A regex literal/string literal.
* @param {boolean} literalFlag - `true` for regex literals.
* @return {object} array - An array of object literals.
*/
let regaxor = (str, re, literalFlag) => {
let flags = '';
let outputs = {'matches': [], 'mismatches': []};
if (!literalFlag) {
re = re.trim();
if (re.startsWith('/') && /\/\w*$/.test(re)) {
let parts = re.split('/');
flags = parts[2];
re = parts[1];
}
}
for (let func of Object.values(regaxorHelpers)) {
let result;
let matchStr = func(str);
if (matchStr === str)
continue;
re = flags ? new RegExp(re, flags) : new RegExp(re);
do {
let dict = {};
result = re.exec(matchStr);
if (result !== null) {
dict.index = result.index;
dict.match = result[0];
dict.input = result.input;
outputs.matches.push(dict);
} else {
outputs.mismatches.push(matchStr);
}
} while (re.lastIndex && result !== null &&
re.lastIndex !== matchStr.lastIndexOf(result) + 1);
}
return outputs;
};