-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmini-regexp101.html
More file actions
389 lines (366 loc) · 10.8 KB
/
mini-regexp101.html
File metadata and controls
389 lines (366 loc) · 10.8 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>mini-RegExp</title>
<!-- <script src="https://cdn.tailwindcss.com"></script> -->
<script src="lib/tailwindcss.js"></script>
<style>
input, button {
outline: none;
}
#result strong {
font-weight: normal;
&:nth-of-type(odd) {
background-color: #9fcfff;
}
&:nth-of-type(even) {
background-color: #d5ebff;
}
}
#output {
position: relative;
width:300px;
height: 200px;
> textarea, #result {
font-family: consolas;
width: 100%;
height: 100%;
white-space: pre-wrap;
word-break: break-all;
border: 1px solid;
overflow: auto;
/* position: absolute; */
background-color: transparent;
}
}
div[contenteditable] {
/*-webkit-spellcheck: false;*/
}
#result > em:nth-of-type(odd) {
}
#result > em:nth-of-type(even) {
opacity: 0.7;
}
em {
font-style: normal;
}
em:not(:has(:hover)):hover {
outline: 2px solid grey;
}
em:not(:has(:hover)):hover em{
background-color: inherit;
}
[data-group-index="0"] {
background-color: #9fcfff;
}
[data-group-index="1"] {
background-color: #9fcba1;
}
[data-group-index="2"] {
background-color: #e0bf8b;
}
[data-group-index="3"] {
background-color: #acadfc;
}
[data-group-index="4"] {
background-color:#e1abf5;
}
.zero-width-assert {
position: relative;
border-left: 1px dotted red;
margin-left: -1px;
&:hover {
box-shadow: 0px 0px 4px #9fcfff;
outline: 1px solid grey;
border-radius: 2rem;
border-left: 1px solid #9fcfff;
}
}
#group-hover-title {
position: fixed;
background-color: black;
white-space: pre;
color: white;
font-size: 12px;
opacity: 0.8;
padding: 0.5em;
border-radius: 0.5em;
display: none;
transform: translateX(-50%) translateY(-100%) translateY(-5px);
}
</style>
</head>
<body>
<div>
<div>REGULAR EXPRESSION</div>
<input class="border" type="text" id="reInput" value=".(.(.)(.))(..)">
<button class="border" id="runInputButton" onclick="run()">RUN</button>
<span id="matchState"></span>
<div id="tips"></div>
<div>FLAGS</div>
<label><input type="checkbox" id="flag-g" checked> g</label>
<label><input type="checkbox" id="flag-m"> m</label>
<label><input type="checkbox" id="flag-i"> i</label>
<label><input type="checkbox" id="flag-y"> y</label>
<label><input type="checkbox" id="flag-u"> u</label>
<label><input type="checkbox" id="flag-s"> s</label>
<label><input type="checkbox" id="flag-d" checked> d</label>
<label><input type="checkbox" id="flag-v"> v</label>
<div>TEST STRING</div>
<div id="output">
<div contenteditable id="result" spellcheck="false">
An explanation of your regex will be automatically generated as you type.
An explanation of your regex will be automatically generated as you type.
3 chickens
55 cows
100 eggs
1 dragonfly
1 pig
</div>
</div>
<div id="group-hover-title"></div>
</div>
<script>
"use strict";
//元素id为xxx时,页面的全局就会出现一个叫xxx的变量,指向一个想表示那个标签的对象
let result = document.querySelector("#result")
let reInput = document.querySelector("#reInput")
let runInputBtn = document.querySelector("#runInputButton")
let tips = document.querySelector("#tips")
let matchState = document.querySelector("#matchState")
let groupHoverTitle = document.querySelector("#group-hover-title")
window.addEventListener("load", run)
result.addEventListener("input", debounce(run, 500))
reInput.addEventListener("keyup", debounce(run, 500))
runInputBtn.addEventListener("lick", run)
result.addEventListener("mousemove", showMatchGroupInfo)
result.addEventListener("mouseleave", hiddenMatchGroupInfo, true)
let lastStartTimeOfMatch = 0
function run(e) {
let matchTime = Date.now()
lastStartTimeOfMatch = matchTime
let reString = reInput.value // 可以通过value属性读取到文本框的值
if (reString === "") {
return undefined
}
let flags = getFlags()
let re = null
try{
re = new RegExp(reString, flags)
} catch(e) {
if (e instanceof SyntaxError) {
tips.innerHTML = e.message
return undefined
} else {
throw e
}
}
tips.innerHTML = ""
let testString = result.textContent
let lastLastIndex = 0
let html = ""
let matchIndex = 0
getMatchesFromWorker(re, testString, matches => {
let position = 0
if (matchTime < lastStartTimeOfMatch) {
return
}
matches.forEach(match => {
html += testString.slice(lastLastIndex, match.index)
html += heightLightRegExp(match, matchIndex)
lastLastIndex = match.indices[0][1]
matchIndex++
})
html += testString.slice(lastLastIndex)
if (e.target === result) {
position = getCursorPosition(result)
}
result.innerHTML = html // 这里用的不是value属性,而是innerHTML
if (e.target === result) {
restoreCursorPosition(result, position)
}
}, matchTime)
}
function showMatchGroupInfo(e) {
if(e.target.matches("em")) {
let emBoxes = e.target.getClientRects()
// console.dir(emBoxes)
let mouseX = e.clientX
let mouseY = e.clientY
// console.log(mouseX)
let box = getMouseHoverBox(emBoxes, mouseY)
// console.dir(box)
groupHoverTitle.textContent = e.target.dataset.title
// console.log(e.target.dataset.title)
groupHoverTitle.style.top = box.top + "px"
groupHoverTitle.style.left = mouseX + "px"
groupHoverTitle.style.display = "block"
}
}
function hiddenMatchGroupInfo(e) {
console.dir(e)
if(e.target.matches("em")) {
console.log(111)
groupHoverTitle.style.display = "none"
}
}
function getMouseHoverBox(boxes, y) {
if(boxes.length === 1) {
return boxes[0]
}
for (let i = 0; i < boxes.length; i++) {
if (boxes[i].top < y && boxes[i].bottom > y) {
return boxes[i]
}
}
return boxes[0]
}
function debounce(func, delay) {
let timeId = null
return function (...arg) {
clearTimeout(timeId)
timeId = setTimeout(() => {
func(...arg)
}, delay)
}
}
function restoreCursorPosition(node, input) {
let selected = window.getSelection()
traverseTextNode(node, (textNode) => {
if (input > textNode.textContent.length) {
input -= textNode.textContent.length
} else {
selected.setPosition(textNode, input)
return false
}
})
}
function getCursorPosition(node) {
let selected = window.getSelection()
let pos = 0
traverseTextNode(node, (textNode) => {
if (textNode === selected.anchorNode) {
pos += selected.anchorOffset
return false
} else {
pos += textNode.textContent.length
}
})
return pos
}
function traverseTextNode(node, func) {
if (node.nodeType === document.TEXT_NODE) {
if(func(node) === false) {
return false
}
} else if (node.nodeType === document.ELEMENT_NODE) {
for (let i = 0; i < node.childNodes.length; i++) {
if (traverseTextNode(node.childNodes[i], func) === false) {
return false
}
}
}
}
function matchOk(matchTime) {
if(matchTime < lastStartTimeOfMatch) {
return
}
matchState.textContent = "okk"
}
function matchLoad(matchTime) {
if(matchTime < lastStartTimeOfMatch) {
return
}
matchState.textContent = "loading..."
}
function matchTimeout(matchTime) {
if(matchTime < lastStartTimeOfMatch) {
return
}
matchState.textContent = "timeout"
}
function getMatchesFromWorker(re, textString, func, matchTime) {
let isAction = false
let worker = new Worker("regexp-match-worker.js")
worker.postMessage({
regexp: re,
string:textString
})
let matches = null
worker.addEventListener("message", e => {
worker.terminate()
matches = e.data
func(matches)
isAction = true
matchOk(matchTime)
})
setTimeout(() => {
if (isAction === false) {
matchLoad(matchTime)
}
},500)
setTimeout(() => {
if (isAction === false) {
worker.terminate()
matchTimeout(matchTime)
}
},4000)
}
function heightLightRegExp(match, matchIndex) {
let info = null
let result = ""
if (match[0].length === 0) { //说明是零宽断言
info = [
`Match ${matchIndex + 1}`,
`----------`,
`Group 0: empty string`,
`Pos: ${match.index}-${match.index}`
].join("\n")
result += `<u class="zero-width-assert" title="${info}"></u>`
return result
}
let start = 0
let end = 0
let label = new Array(match[0].length + 1).fill("")
let groupIndex = 0
for (let index of match.indices) {
info = [
`Match ${matchIndex + 1}`,
`----------`,
`Group ${groupIndex}: ${match[groupIndex]}`,
`Pos: ${index[0]}-${index[1]}`
].join("\n")
start = index[0] - match.index
end = index[1] - match.index
label[start] += `<em data-group-index="${groupIndex % 8}" data-title="${info}">`
label[end] = "</em>" + label[end]
groupIndex++
}
for (let i = 0; i < match[0].length; i++) {
result += label[i] + match[0][i]
}
result += label[label.length - 1]
return result
}
function getFlags() {
let flags = ['g', 'm', 'i', 'y', 'u', 's', 'd', 'v']
return flags.filter(it => {
let element = document.getElementById("flag-" + it)
if (element.checked) { // 对于checkbox来说,checked属性可以返回它是否有被打上勾
return true
} else {
return false
}
}).join('')
}
function syncScroll() {
result.scrollTop = testStringInput.scrollTop
}
function syncScroll2() {
testStringInput.scrollTop = result.scrollTop
}
</script>
</body>
</html>