-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
87 lines (74 loc) · 1.88 KB
/
test.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
str.replace(/c|[a-z]?C[a-z]?/g, '')
str.toLowerCase().split('').sort().join('').match(/(.)\1+/g || []).length
try {
return str.toLowerCase().split('').sort().join('').match(/(.)\1+/g || []).length
} catch (e) {
return 0
}
function trim(str, c) {
var escapedc = c.replace(/(?=[^\w ])/g, '\\')
var re = new RegExp(`^${escapedc}+|${escapedc}+$`, 'gi')
return str.replace(re, '')
}
function isMatch(str, w) {
w = w.replace(/(?=[^\w ])(?![?*])/g, '\\')
.replace(/\?/g, '.')
.replace(/\*/g, '.*')
var re = new RegExp('^' + w + '$')
return re.test(str)
}
function test(re, str) {
if (re.exec)
}
function search(str, re) {
var match = re.exec(str)
}
function match(str, re) { // 不考虑 lastIndex
if (re.global) {
let result = []
} else {
return re.exec(str)
}
}
function split(str, re) {
var result = []
var lastIndex = 0
re = new RegExp(re.source, re.flags + 'g')
var match
while (match = re.exec(str)) {
result.push(str.slice(lastIndex, match.index))
result.push(...match.slice(1))
lastIndex = re.lastIndex
}
result.push(str.slice(lastIndex))
return result
}
str.replace(re, f)
function replace(str, re, f) {
var result = ''
var lastIndex = 0
re = new RegExp(re.source, re.flags.contains('g')?re.flags : re.flags + 'g')
var match
while (match = re.exec(str)) {
result += str.slice(lastIndex, match.index)
result += f(...match)
lastIndex = re.lastIndex
}
result += str.slice(lastIndex)
return result
}
replace('fooboocoo',/oo/,function(m)){return m.toUpperCase()}
// =>
function replace(str, re, f) {
var result = ''
var lastIndex = 0
re = new RegExp(re.source, re.flags.contains('g') ? re.flags : re.flags + 'g')
var match
while(match = re.exec(str)) {
result += str.slice(lastIndex, match.index)
result += f(...match)
lastIndex = re.lastIndex
}
result += str.slice(lastIndex)
return result
}