User:Gerbrant/edit/multiReplace.js
From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Firefox/Mozilla/Safari: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera/Konqueror: press F5.
module("Gerbrant.edit.multiReplace", function() { patterns = []; handlers = []; indices = []; var j = 1; this.add = function(p, h) { var n = p.match(/\((?!\?)/g); if(n) n = n.length + j; else n = j; patterns.push(p.replace(/\\(\d+)/, function(a, b) { return "\\" + (+b + n - 1); })); handlers.push(h); indices.push([j, ++n]); j = n; } //equivalent aan a.slice(b, e), maar dat werkt niet voor arguments function sliceArg(a, b, e) { n = []; while(b < e) n.push(a[b++]); return n; } this.handler = function(a) { var i, idx; for(i in indices) { idx = indices[i]; if(arguments[idx[0]] != undefined) try { return handlers[i].apply(a, sliceArg(arguments, idx[0], idx[1])); } catch(e) { alert(e.message + "\n\n" + handlers[i]); return a; } } throw "Failed sanity check."; } this.getRE = function() { return new RegExp("(" + patterns.join(")|(") + ")", "g") }; this.replace = function(t) { return t.replace(this.getRE(), this.handler); }; });