-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex.js
More file actions
68 lines (53 loc) · 2.04 KB
/
regex.js
File metadata and controls
68 lines (53 loc) · 2.04 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
/* regex.js
// giovanni 2015
// making use of the javascript regex engine
// to test a regular expression on a given text input
// beta version installed on:
// http://galunni.github.io/tools/regex_tester.html
*/
var testo = "";
var activeFunction = "match";
function adoptText(){
testo =
document.getElementById('textOutput').innerHTML =
document.getElementById('textInput').value;
updateResults();
}
function updateResults(){
if(activeFunction === "match"){
matchIt();
} else if(activeFunction === "subst"){
substituteIt();
} else if(document.forms.formulario.testo_sub_1.value.length >= 1) {
substituteIt();
} else if(document.forms.formulario.testo_match.value.length >= 1) {
matchIt();
}
}
function getFlags(){
var flags = ""; // reading modifiers
flags += (document.forms.formulario.global_flag.checked) ? "g" : ""; // global
flags += (document.forms.formulario.ignore_flag.checked) ? "i" : ""; // ignore case
flags += (document.forms.formulario.multi_flag.checked) ? "m" : ""; // multiline
return flags;
}
function matchIt(){
activeFunction = "match";
var rr =
eval("/" + document.forms.formulario.testo_match.value + "/" + getFlags());
document.getElementById("textOutput").innerHTML =
testo.replace(rr, '<b class="match">' + "$&" + '</b>');
// update bg color of selected method (match)
document.getElementById("matcher").style.backgroundColor="#8C9C88";
document.getElementById("subster").style.backgroundColor="transparent";
}
function substituteIt(){
activeFunction = "subst";
var r1 = eval("/" + document.forms.formulario.testo_sub_1.value + "/" + getFlags());
var r2 = document.forms.formulario.testo_sub_2.value;
document.getElementById("textOutput").innerHTML =
testo.replace(r1, '<b class="subst">' + r2 + '</b>');
// update bg color of selected method (substitute)
document.getElementById("subster").style.backgroundColor="#F2DBAE";
document.getElementById("matcher").style.backgroundColor="transparent";
}