-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathRegex.js
More file actions
103 lines (94 loc) · 2.12 KB
/
Regex.js
File metadata and controls
103 lines (94 loc) · 2.12 KB
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
export const showRegexImpl = function (r) {
return "" + r;
};
export const regexImpl = function (left) {
return function (right) {
return function (s1) {
return function (s2) {
try {
return right(new RegExp(s1, s2));
} catch (e) {
return left(e.message);
}
};
};
};
};
export const source = function (r) {
return r.source;
};
export const flagsImpl = function (r) {
return {
multiline: r.multiline,
ignoreCase: r.ignoreCase,
global: r.global,
dotAll: r.dotAll,
sticky: !!r.sticky,
unicode: !!r.unicode
};
};
export const test = function (r) {
return function (s) {
var lastIndex = r.lastIndex;
var result = r.test(s);
r.lastIndex = lastIndex;
return result;
};
};
export const _match = function (just) {
return function (nothing) {
return function (r) {
return function (s) {
var m = s.match(r);
if (m == null || m.length === 0) {
return nothing;
} else {
for (var i = 0; i < m.length; i++) {
m[i] = m[i] == null ? nothing : just(m[i]);
}
return just(m);
}
};
};
};
};
export const replace = function (r) {
return function (s1) {
return function (s2) {
return s2.replace(r, s1);
};
};
};
export const _replaceBy = function (just) {
return function (nothing) {
return function (r) {
return function (f) {
return function (s) {
return s.replace(r, function (match) {
var groups = [];
var group, i = 1;
while (typeof (group = arguments[i++]) !== "number") {
groups.push(group == null ? nothing : just(group));
}
return f(match)(groups);
});
};
};
};
};
};
export const _search = function (just) {
return function (nothing) {
return function (r) {
return function (s) {
var result = s.search(r);
return result === -1 ? nothing : just(result);
};
};
};
};
export const split = function (r) {
return function (s) {
return s.split(r);
};
};