User:Gimmetrow/regexp.js

From Wikipedia, the free encyclopedia

If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
//<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);
    }
}
 
function soloYears() {
   var monthyear = new RegExp("\\[\\[(January|February|March|April|May|June|July|August|September|October|November|December),? *([0-9][0-9][0-9][0-9])\\]\\]", "g");
   var txt = document.editform.wpTextbox1;
   txt.value = txt.value.replace(/([^,\]] |[^,\] ])\[\[([0-9][0-9][0-9][0-9])\]\]/g, "$1$2");
   txt.value = txt.value.replace(monthyear, "$1 $2");
   var summary = document.editform.wpSummary;
   //summary.value = summary.value + " [[MOS:DATE]] ";
}
 
function dayMonth(option) {
   var daymonth = new RegExp("\\[\\[([0-3][0-9]|[0-9]) (January|February|March|April|May|June|July|August|September|October|November|December)\\]\\],? *\\[\\[([0-9][0-9][0-9][0-9])\\]\\]", "g");
   var monthday = new RegExp("\\[\\[(January|February|March|April|May|June|July|August|September|October|November|December) ([0-3][0-9]|[0-9])\\]\\],? *\\[\\[([0-9][0-9][0-9][0-9])\\]\\]", "g");
   var daymonth2 = new RegExp("\\[\\[([0-3][0-9]|[0-9]) (January|February|March|April|May|June|July|August|September|October|November|December)\\]\\]", "g");
   var monthday2 = new RegExp("\\[\\[(January|February|March|April|May|June|July|August|September|October|November|December) ([0-3][0-9]|[0-9])\\]\\]", "g");
 
   var txt = document.editform.wpTextbox1;
   if (option == 1) {
      // dd Month
      txt.value = txt.value.replace(monthday , "[[$2 $1]] [[$3]]");
      txt.value = txt.value.replace(daymonth , "[[$1 $2]] [[$3]]");
      txt.value = txt.value.replace(monthday2, "[[$2 $1]]");
      txt.value = txt.value.replace(daymonth2, "[[$1 $2]]");
   } else {
      // Month dd
      txt.value = txt.value.replace(monthday , "[[$1 $2]], [[$3]]");
      txt.value = txt.value.replace(daymonth , "[[$2 $1]], [[$3]]");
      txt.value = txt.value.replace(monthday2, "[[$1 $2]]");
      txt.value = txt.value.replace(daymonth2, "[[$2 $1]]");
   }
}
 
function addDLinks() {
   var daymonth = new RegExp("([0-3][0-9]|[0-9]) (January|February|March|April|May|June|July|August|September|October|November|December),? *([0-9][0-9][0-9][0-9])", "g");
   var monthday = new RegExp("(January|February|March|April|May|June|July|August|September|October|November|December) ([0-3][0-9]|[0-9]),? *([0-9][0-9][0-9][0-9])", "g");
   var daymonth2 = new RegExp("\\[\\[([0-3][0-9]|[0-9]) (January|February|March|April|May|June|July|August|September|October|November|December)\\]\\],? *([0-9][0-9][0-9][0-9])", "g");
   var monthday2 = new RegExp("\\[\\[(January|February|March|April|May|June|July|August|September|October|November|December) ([0-3][0-9]|[0-9])\\]\\],? *([0-9][0-9][0-9][0-9])", "g");
   var txt = document.editform.wpTextbox1;
   txt.value = txt.value.replace(monthday, "[[$1 $2]], [[$3]]");
   txt.value = txt.value.replace(daymonth, "[[$1 $2]] [[$3]]");
   txt.value = txt.value.replace(monthday2, "[[$1 $2]], [[$3]]");
   txt.value = txt.value.replace(daymonth2, "[[$1 $2]] [[$3]]");
}
 
function remDLinks() {
   var daymonth = new RegExp("\\[\\[([0-3][0-9]|[0-9]) (January|February|March|April|May|June|July|August|September|October|November|December)\\]\\],? *\\[\\[([0-9][0-9][0-9][0-9])\\]\\]", "g");
   var monthday = new RegExp("\\[\\[(January|February|March|April|May|June|July|August|September|October|November|December) ([0-3][0-9]|[0-9])\\]\\],? *\\[\\[([0-9][0-9][0-9][0-9])\\]\\]", "g");
   var txt = document.editform.wpTextbox1;
   txt.value = txt.value.replace(monthday, "$1 $2, $3");
   txt.value = txt.value.replace(daymonth, "$1 $2 $3");
}
 
function emptyParm() {
   var txt = document.editform.wpTextbox1;
   txt.value = txt.value.replace(/\|[\n]*[ ]*[a-zA-Z][a-zA-Z0-9]*[ ]*=[ ]*[\n]*[ ]*\|/g, "|");
   txt.value = txt.value.replace(/\|[\n]*[ ]*[a-zA-Z][a-zA-Z0-9]*[ ]*=[ ]*[\n]*[ ]*}}/g, "}}");
}
 
function unspRefs() {
   var txt = document.editform.wpTextbox1;
   txt.value = txt.value.replace(/(<ref[^>]*>)[ ]*/g, "$1");
   txt.value = txt.value.replace(/(<ref[^>]*\/>)[ ]*/g, "$1 ");
   txt.value = txt.value.replace(/[ ]+<\/ref>[ ]*/g, "</ref> ");
}
 
function formTemp() {
   var txt = document.editform.wpTextbox1;
   txt.value = txt.value.replace(/[ ]*\|[ ]*\n/g, "\n|");
   txt.value = txt.value.replace(/\n\|[ ]*/g, "\n|");
}
 
function formTem2() {
   var txt = document.editform.wpTextbox1;
   txt.value = txt.value.replace(/\n[ ]*\|[ ]*/g, "\n| ");
}
 
function regOpt1() {
   var txt = document.editform.wpTextbox1;
   txt.value = txt.value;
}
 
function defSort() {
   var txt = document.editform.wpTextbox1;
   txt.value = txt.value.replace(/(\[Category:[^\|]*)\|[^ \]][^\]]*\]/g, "$1]");
}
 
function wards() {
   var txt = document.editform.wpTextbox1;
   txt.value = txt.value.replace(/style="text-align:center;background: #[Dd][Dd][Ff][Ff][Dd][Dd];vertical-align: middle;"\|[ ]*([^\n]*)/g, "{{award-won|$1}}");
   txt.value = txt.value.replace(/style="text-align:center;background: #[Ff][Ff][Dd][Dd][Dd][Dd];vertical-align: middle;"\|[ ]*([^\n]*)/g, "{{award-nom|$1}}");
   txt.value = txt.value.replace(/style="text-align:center;background: #[Dd][Dd][Ff][Ff][Dd][Dd]"\|[ ]*([^\n]*)/g, "{{award-won|$1}}");
   txt.value = txt.value.replace(/style="text-align:center;background: #[Ff][Ff][Dd][Dd][Dd][Dd]"\|[ ]*([^\n]*)/g, "{{award-nom|$1}}");
   txt.value = txt.value.replace(/align="center" style="background: #[Dd][Dd][Ff][Ff][Dd][Dd]"\|[  ']*Won[']*/g, "{{award-won}}");
   txt.value = txt.value.replace(/align="center" style="background: #[Ff][Ff][Dd][Dd][Dd][Dd]"\|[ ]*Nominated/g, "{{award-nom}}");
   txt.value = txt.value.replace(/style="background: #[Dd][Dd][Ff][Ff][Dd][Dd]"\|[  ']*Won[']*/g, "{{award-won}}");
   txt.value = txt.value.replace(/style="background: #[Ff][Ff][Dd][Dd][Dd][Dd]"\|[ ]*Nominated/g, "{{award-nom}}");
}
 
addOnloadHook(function () {
    if(document.forms.editform) {
        addPortletLink('p-tb', 'javascript:regExp()'   , 'Regexp replace'   , 't-regexp',   'Regexp replace', '', '');
        addPortletLink('p-tb', 'javascript:addDLinks()', 'Format +datelinks', 't-monthday', 'Regexp links', '', '');
        addPortletLink('p-tb', 'javascript:dayMonth(1)', 'Format \[\[DMY]]' , 't-daymonth', 'Regexp day month', '', '');
        addPortletLink('p-tb', 'javascript:dayMonth(2)', 'Format \[\[MDY]]' , 't-monthday', 'Regexp month day', '', '');
        addPortletLink('p-tb', 'javascript:remDLinks()', 'Format -datelinks', 't-monthday', 'Regexp links', '', '');
        addPortletLink('p-tb', 'javascript:soloYears()', 'Delink \[\[Year]]', 't-solo',     'Regexp years', '', '');
        addPortletLink('p-tb', 'javascript:defSort()'  , 'Defsort clear'    , 't-defsort',  'Regexp defsort', '', '');
        addPortletLink('p-tb', 'javascript:emptyParm()', 'Empty params'     , 't-emptyParm','Regexp unused params', '', '');
        addPortletLink('p-tb', 'javascript:formTemp()' , 'Format template'  , 't-formTemp', 'Regexp format temp', '', '');
        addPortletLink('p-tb', 'javascript:formTem2()' , 'Format template2' , 't-formTem2', 'Regexp format tem2', '', '');
        addPortletLink('p-tb', 'javascript:wards()'    , 'Awards'           , 't-awards','Regexp awards', '', '');
        addPortletLink('p-tb', 'javascript:regOpt1()'  , 'Reg Opt1'         , 't-regopt1',  'Regexp Opt1', '', '');
        addPortletLink('p-tb', 'javascript:unspRefs()' , 'Unspace refs'     , 't-unspRefs', 'Regexp unspace refs', '', '');
    }
});
//</code></pre>