-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex-petite-vue.js
57 lines (57 loc) · 1.8 KB
/
regex-petite-vue.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
function regex_matches(regex, input, regex_flags) {
try {
query = new RegExp(regex, regex_flags);
return input.matchAll(query);
} catch (error) {
console.log("invalid regexp: " + regex);
}
}
function marked(regex, input, regex_flags) {
const matches = regex_matches(regex, input, regex_flags);
let output = "";
let cur = 0;
if (matches == undefined) return;
for (const match of matches) {
output += input.slice(cur, match.index) + `<mark>${match[0]}</mark>`;
cur = match.index + match[0].length;
//console.log( `Found ${match[0]} start=${match.index} end=${match.index + match[0].length}.`,);
}
if (cur < input.length) {
output += input.slice(cur);
}
return output;
}
function add_dowload_data(matches, download_id) {
var csv = "";
var link = document.getElementById(download_id);
//console.log(matches);
for (match of matches) {
csv += match[0] + "\n";
}
console.log(csv);
data = new Blob([csv]);
var url = URL.createObjectURL(data);
if (link != undefined) link.href = url;
}
function matches(regex, input, regex_flags) {
const matches = regex_matches(regex, input, regex_flags);
let output = "<ul>";
if (matches == undefined) return;
for (const match of matches) {
let matchArr = [...match];
console.log(matchArr, matchArr.length);
output += `<li class ="matched">${matchArr[0]}`;
/* if (matchArr.length > 1){
output += "<ol>";
for (let i = 1; i++; i<matchArr.length){
if (matchArr[i] == undefined){console.log(matchArr[i]);break;}
output += `<li class ="matched">${matchArr[i]}</li>`
}
output += "</ol>";
} */
output += "</li>";
}
output += "</ul";
add_dowload_data(regex_matches(regex, input, regex_flags), "download");
return output;
}