பயனர்:Trengarasu.edit.regexReplace
Appearance
hookEvent("load", function() {
var lang, textBox = document.getElementById("wpTextbox1"); if(!textBox) return; try{ lang = Gerbrant.edit.regexReplace.lang; }catch(nopresets){} if(!lang) lang = wgUserLanguage; switch(lang) {
// Localized strings:
case "nl": str = {
finished: "Voltooid; $1 items werden vervangen.", finished2: "Voltooid; $1 van $2 items werden vervangen.", notFound: "De gevraagde tekst is niet gevonden.", replace: "Vervangen...", replaceSel: "Vervang selectie", errorNoCheckBox: "Kan het corresponderende keuzevakje niet vinden.", useRepFun: "< functie >"
}; break;
default: str = {
finished: "Finished; $1 items were replaced.", finished2: "Finished; $1 out of $2 items were replaced.", notFound: "Requested text was not found.", replace: "Replace...", replaceSel: "Replace selection", errorNoCheckBox: "Cannot find the corresponding checkbox.", useRepFun: "< function >"
};
// Don't localize anything beyond this point.
}
var regex, text, minFrag = 30, minDel = 10, repFun;
var results = document.createElement("DIV"); var tbRegex = document.createElement("INPUT"); //tbRegex.value = "[A-Z]{2,}";
var hl = document.createElement("INPUT"); hl.type = "checkbox";
var rep = document.createElement("DIV"); rep.style.display = "none";
function findMatches() { var m = [], r = [], i = 0; regex = new RegExp(tbRegex.value, hl.checked ? "g" : "ig"); text = textBox.value;
var t = text.replace(regex, function(a) { m.push(a); return "\u0001"; }).split("\u0001");
if(m.length == 0) { results.innerHTML = str.notFound; return; }
if(t[0].length > minFrag + minDel) { r.push(" \u2026 "); r.push(t[0].slice(-minFrag)); } else r.push(t[0]); while(true) { r.push('\u0001b>\u0001input type="checkbox" checked id="Gerbrant-errCB'); r.push(i); r.push('">'); r.push(m[i]); r.push('\u0001/b>'); if(++i >= m.length) break; if(t[i].length > 2 * minFrag + minDel) { r.push(t[i].slice(0, minFrag)); r.push(" \u2026 "); r.push(t[i].slice(-minFrag)); } else r.push(t[i]); } if(t[i].length > minFrag + minDel) { r.push(t[i].slice(0, minFrag)); r.push(" \u2026 "); } else r.push(t[i]);
rep.style.display = ""; results.innerHTML = r.join("").replace( /&/g, "&").replace( /</g, "<").replace( /\u0001/g, "<").replace( /\n[ \t]/g, "\n ").replace( /[ \t](?=[ \t])/g, " ").replace( /\n/g, "
"); }
var ok = document.createElement("INPUT"); ok.type = "button"; ok.value = str.replace; ok.onclick = findMatches;
var div = document.createElement("DIV"); div.appendChild(tbRegex); div.appendChild(hl); div.appendChild(ok);
var tbReplace = document.createElement("INPUT"); //tbReplace.value = "function(a){return a.slice(0,1) + a.slice(1).toLowerCase();}"; var chkJS = document.createElement("INPUT"); chkJS.type = "checkbox";
function doReplace() { var rjs = chkJS.checked, i = 0, j = 0, r, nt; var ohno = false; r = tbReplace.value; if(rjs) if(r == str.useRepFun) r = repFun; else eval("r=" + r); nt = text.replace(regex, function(a) { var c = document.getElementById("Gerbrant-errCB" + i++); if(c) if(c.checked) { j++; if(rjs) try { return r.apply(RegExp, arguments); } catch(e) { alert(e.message); return a; } else return r; } else return a; else ohno = true; }); if(ohno) { alert(str.errorNoCheckBox); return; } if(j < i) results.innerHTML = str.finished2.replace("$1", j).replace("$2", i); else results.innerHTML = str.finished.replace("$1", j); rep.style.display = "none"; textBox.value = nt; }
var finish = document.createElement("INPUT"); finish.type = "button"; finish.value = str.replaceSel; finish.onclick = doReplace;
rep.appendChild(tbReplace); rep.appendChild(chkJS); rep.appendChild(finish);
textBox.parentNode.insertBefore(div, textBox); textBox.parentNode.insertBefore(rep, textBox); textBox.parentNode.insertBefore(results, textBox);
function onResize() { finish.style.height = ok.style.height = tbRegex.offsetHeight; tbRegex.style.width = textBox.offsetWidth - ok.offsetWidth - hl.offsetWidth; tbReplace.style.width = textBox.offsetWidth - finish.offsetWidth - chkJS.offsetWidth; } onResize(); hookEvent("resize", onResize);
if(typeof module == "function") module("Gerbrant.edit.regexReplace", {replace: function(what, ulcase, repl, isjs) { tbRegex.value = what; hl.checked = ulcase; if(isjs == undefined && typeof repl == "function") { repFun = repl; tbReplace.value = str.useRepFun; chkJS.checked = true; } else { tbReplace.value = repl; chkJS.checked = isjs; } findMatches(); }});
});
if(typeof loadModule == "function") {
new function() { var presets; try{ presets = Gerbrant.edit.regexReplace.presets; }catch(nopresets){} if(presets) loadModule("Gerbrant.gui.lilink", function(l) { hookEvent("load", function() { var i, p, e = function(s) { return s.toString().replace(/["\\]/g, function(a) { return "\\" + a; }); }; if(!document.getElementById("wpTextbox1")) return; for(i in presets) { p = presets[i]; l.addToolboxLink('javascript:Gerbrant.edit.regexReplace.replace("' + e(p.regex) + '",' + !!p.caseSens + ',"' + e(p.replace) + '",' + (typeof(p.replace) == "function") + ');', i); } }); }); };
}