-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
66 lines (55 loc) · 1.92 KB
/
index.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
// (c) 2007 Steven Levithan <stevenlevithan.com>
// MIT License
/*** matchRecursive
accepts a string to search and a format (start and end tokens separated by "...").
returns an array of matches, allowing nested instances of format.
examples:
matchRecursive("test", "(...)") -> []
matchRecursive("(t(e)s)()t", "(...)") -> ["t(e)s", ""]
matchRecursive("t<e>>st", "<...>") -> ["e"]
matchRecursive("t<<e>st", "<...>") -> ["e"]
matchRecursive("t<<e>>st", "<...>") -> ["<e>"]
matchRecursive("<|t<e<|s|>t|>", "<|...|>") -> ["t<e<|s|>t"]
*/
module.exports = (function () {
var formatParts = /^([\S\s]+?)\.\.\.([\S\s]+)/,
metaChar = /[-[\]{}()*+?.\\^$|,]/g;
function escape (str) {
return str.replace(metaChar, "\\$&");
}
function validateParts(p) {
if (!p) {
throw new Error("format must include start and end tokens separated by '...'");
}
if (p[1] === p[2]) {
throw new Error("start and end format tokens cannot be identical");
}
}
return function (str, format) {
var p = formatParts.exec(format);
validateParts(p);
var opener = p[1],
closer = p[2],
/* Use an optimized regex when opener and closer are one character each */
iterator = new RegExp(format.length === 5 ? "["+escape(opener+closer)+"]" : escape(opener)+"|"+escape(closer), "g"),
results = [],
openTokens, matchStartIndex, match;
do {
openTokens = 0;
while (match = iterator.exec(str)) {
if (match[0] === opener) {
if (!openTokens) {
matchStartIndex = iterator.lastIndex;
}
openTokens++;
} else if (openTokens) {
openTokens--;
if (!openTokens) {
results.push(str.slice(matchStartIndex, match.index));
}
}
}
} while (openTokens && (iterator.lastIndex = matchStartIndex));
return results;
};
})();