User:Gimmetrow/replace.js
From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Mozilla/Safari: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera/Konqueror: press F5.
//<pre><code> // This code requires addLink from: User:Omegatron/monobook.js/addlink.js // // Based on: User:Trilobite/Tools, User:Omegatron/monobook.js/replacetab.js function regExp() { var s = prompt("Search regexp?"); if(s) { var r = prompt("Replace regexp?"); if(!r && r != '') return; var txt = document.editform.wpTextbox1; txt.value = txt.value.replace(new RegExp(s, "g"), r); } } // User:Gimmetrow/replace.js function fixRefMarks() { var qt = String.fromCharCode(34) // var hb = String.fromCharCode(124) var txt = document.editform.wpTextbox1; // fact and other templates (geographic reference template, ref/note) var facttags = "({{[ ]*fact[ ]*}}|{{[ ]*citequote[ ]*}}|{{[ ]*citation needed[ ]*}}|{{[ ]*cn[ ]*}}|{{[ ]*verification needed[ ]*}}|{{[ ]*verify source[ ]*}}|{{[ ]*GR[ ]*[\|][ ]*[^ ]+[ ]*}}|{{[ ]*[c]?ref[ ]*[\|][^}]*}}|{{[ ]*ref[ _]label[ ]*[\|][^}]*}})"; txt.value = txt.value.replace(new RegExp("[\n\r\f\t ]+?"+facttags, "gi"), "$1"); // one+ space/linefeed, avoid matching references tag as a named ref txt.value = txt.value.replace(new RegExp("[\n\r\f\t ]+?<ref([ >])", "gi"), "<ref$1"); // remove trailing spaces from named refs txt.value = txt.value.replace(new RegExp("<ref ([^>]*[^> ])[ ]*>", "gi"), "<ref $1>"); // remove some superscripted punctuation between refs (is between necessary?) txt.value = txt.value.replace(new RegExp("(</ref>|<ref[^>]*?/>)<sup>[ ]*[,;-]?[ ]*</sup><ref", "gi"), "$1<ref"); var closetag = "(([^<]|<[^/]|</[^r]|</r[^e]|</re[^f]|</ref[^>])*?)</ref>"; for (var j=0; j<10; j++) // repeat for multiple refs together { // fact tags txt.value = txt.value.replace(new RegExp("([^\.,;:!\?"+qt+"'’])([" + qt + "'’]*)"+facttags+"[ ]*([\.,;:!\?])", "gi"), "$1$2$4$3"); txt.value = txt.value.replace(new RegExp("([!\?])([" + qt + "'’]*)"+facttags+"[ ]*([,;:!\?])", "gi"), "$1$2$4$3"); txt.value = txt.value.replace(new RegExp("([\.,;:])([" + qt + "'’]*)"+facttags+"[ ]*([\.,;:!\?])", "gi"), "$2$4$3"); txt.value = txt.value.replace(new RegExp("([!\?])([" + qt + "'’]*)"+facttags+"[ ]*([\.])", "gi"), "$1$2$3"); // ref followed by punctuation, perhaps preceeded by punctuation (with or without quote) // may contain other tags (eg <cite></cite> txt.value = txt.value.replace(new RegExp("([^\.,;:!\?"+qt+"'’])([" + qt + "'’]*)<ref>"+closetag+"[ ]*([\.,;:!\?])", "gi"), "$1$2$5<ref>$3</ref>"); txt.value = txt.value.replace(new RegExp("([!\?])([" + qt + "'’]*)<ref>"+closetag+"[ ]*([,;:!\?])", "gi"), "$1$2$5<ref>$3</ref>"); txt.value = txt.value.replace(new RegExp("([\.,;:])([" + qt + "'’]*)<ref>"+closetag+"[ ]*([\.,;:!\?])", "gi"), "$2$5<ref>$3</ref>"); txt.value = txt.value.replace(new RegExp("([!\?])([" + qt + "'’]*)<ref>"+closetag+"[ ]*([\.])", "gi"), "$1$2<ref>$3</ref>"); // named ref with content, may contain other tags txt.value = txt.value.replace(new RegExp("([^\.,;:!\?"+qt+"'’])([" + qt + "'’]*)<ref([^>]*?[^/])>"+closetag+"[ ]*([\.,;:!\?])", "gi"), "$1$2$6<ref$3>$4<\/ref>"); txt.value = txt.value.replace(new RegExp("([!\?])([" + qt + "'’]*)<ref([^>]*?[^/])>"+closetag+"[ ]*([,;:!\?])", "gi"), "$1$2$6<ref$3>$4<\/ref>"); txt.value = txt.value.replace(new RegExp("([\.,;:])([" + qt + "'’]*)<ref([^>]*?[^/])>"+closetag+"[ ]*([\.,;:!\?])", "gi"), "$2$6<ref$3>$4<\/ref>"); txt.value = txt.value.replace(new RegExp("([!\?])([" + qt + "'’]*)<ref([^>]*?[^/])>"+closetag+"[ ]*([\.])", "gi"), "$1$2<ref$3>$4<\/ref>"); // named ref re-used txt.value = txt.value.replace(new RegExp("([^\.,;:!\?"+qt+"'’])([" + qt + "'’]*)<ref([^>]*?)/> *?([\.,;:!\?])", "gi"), "$1$2$4<ref$3/>"); txt.value = txt.value.replace(new RegExp("([!\?])([" + qt + "'’]*)<ref([^>]*?)/>[ ]*?([,;:!\?])", "gi"), "$1$2$4<ref$3/>"); txt.value = txt.value.replace(new RegExp("([\.,;:])([" + qt + "'’]*)<ref([^>]*?)/>[ ]*?([\.,;:!\?])", "gi"), "$2$4<ref$3/>"); txt.value = txt.value.replace(new RegExp("([!\?])([" + qt + "'’]*)<ref([^>]*?)/>[ ]*?([\.])", "gi"), "$1$2<ref$3/>"); } var summary = document.editform.wpSummary; //summary.value = summary.value + "+[[WP:FN]] [[User:Gimmetrow/replace.js|fixes]] "; summary.value = summary.value + "+[[WP:FN]] fixes "; } addOnloadHook(function () { if(document.forms.editform) { addLink('p-cactions', 'javascript:regExp()', 'regexp', 'ca-regexp', 'Regexp replace for the edit window', '', 'ca-history'); addLink('p-cactions', 'javascript:fixRefMarks()', 'fix refs', 'ca-reffix', 'Ref fix for the edit window', '', 'ca-history'); } }); //</code></pre>