911c6cf82d9e9ceec21dadd4270c98eb2c18a3d4
3 MochiRegExp: JavaScript Regular Expression Explorer
6 RegExpManager
= function () {
11 RegExpManager
.prototype.initialize
= function () {
13 Fill in the event handlers and sample text, and do the initial update
14 The reason we do the sample text here is so that "clear" really does
17 setNodeAttribute("inp_text", "value", "matched with your pattern");
18 connect("inp_text", "onkeyup", this, "updateSoon");
19 connect("inp_text", "onchange", this, "update");
21 setNodeAttribute("inp_regexp", "value", "/(pattern)/");
22 connect("inp_regexp", "onkeyup", this, "updateSoon");
23 connect("inp_regexp", "onchange", this, "update");
25 connect("regexp_form", "onsubmit", this, "submit");
30 RegExpManager
.prototype.updateSoon
= function () {
32 If the user stops typing for half a second, do one update.
35 this.timer
= callLater(0.5, this.update
);
38 RegExpManager
.prototype.cancelTimer
= function () {
40 Cancel the timer that waits for the user to idle for half a second.
48 RegExpManager
.prototype.update
= function () {
50 Cancel the update timer and actually do an update of the
56 // Evaluate the regexp
57 re
= eval("(" + getElement("inp_regexp").value
+ ")");
59 // If invalid, color it red and stop
60 addElementClass("lab_regexp", "error");
63 // Make sure that it's not red
64 removeElementClass("lab_regexp", "error");
66 // replace the contents of the tbody
67 var match
= getElement("inp_text").value
.match(re
);
68 replaceChildNodes("result_body", this.getRows(match
));
71 RegExpManager
.prototype.getRows
= function (match
) {
73 Return rows for the tbody given a match object
76 // No match, bail with a no match row
77 return TR(null, TD({"colspan": "3"}, "No match!"));
79 var isAlternate
= true;
81 for (var k
in match
) {
82 isAlternate
= !isAlternate
;
83 var trAttribs
= isAlternate
? {"class": "alternate"} : null;
86 // Highlight the result for the input property as three spans:
87 // [beforeMatch, duringMatch, afterMatch]
89 var end
= match
.index
+ match
[0].length
;
91 SPAN(null, v
.substr(0, match
.index
)),
92 SPAN({"class": "highlight"}, v
.substring(match
.index
, end
)),
93 SPAN(null, v
.substr(end
))
97 TR((isAlternate
? {"class": "alternate"} : null),
107 RegExpManager
.prototype.submit
= function () {
112 regExpManager
= new RegExpManager();
113 addLoadEvent(regExpManager
.initialize
);
115 // rewrite the view-source links
116 addLoadEvent(function () {
117 var elems
= getElementsByTagAndClassName("A", "view-source");
118 var page
= "mochiregexp/";
119 for (var i
= 0; i
< elems
.length
; i
++) {
121 var href
= elem
.href
.split(/\//).pop();
122 elem
.target
= "_blank";
123 elem
.href
= "../view-source/view-source.html#" + page
+ href
;