Wikipedia talk:WikiProject User scripts/Scripts/Replace

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Improved code. Added a question for case-insensitive (default) search.

//

/* Adds a "Replace" tab which pops up three prompt boxes:
   one for a regexp, 
   one for case-insensitive search (confirm) or case-sensitive (abort)
   and one for a replacement.
   Source http://en.wikipedia.org/wiki/Wikipedia:WikiProject_User_scripts/Scripts/Replace
*/
function wpTextboxReplace() {
    var s = prompt("Search regexp:");
    if(s){
        var ci = confirm("case insensitive search?");
        var cis = (ci) ? 'i':'';
        var cis2 = (ci) ? 'in':'';
        var r = prompt("Replace /"+s+"/ case-"+cis2+"sensitively with:");
        if(!r && r != '') return;
        var txt = document.editform.wpTextbox1;
        txt.value = txt.value.replace(new RegExp(s, "mg"+cis ), r);
    }
}
addOnloadHook(function () {
    if (document.forms.editform) {
        addPortletLink('p-cactions', 'javascript:wpTextboxReplace()', 'Replace', 'ca-replace',
                       'Regexp replace for the edit window', 'R', document.getElementById('ca-history'));
    }
});
//

Please update the script[edit]

Could you update the code with the one from below. This new version checks if the entered regexp actually matches the text, and if not, asks if the user wants to enter a new regexp again. Especially with complicated regexps, one sometimes just needs a few more tries, and this saves one many clicks. I've tested in my own user skin and it worked without problems. Let me know if you have further questions. Thanks, --The Evil IP address (talk) 15:41, 12 September 2011 (UTC)[reply]

 Done — Martin (MSGJ · talk) 15:51, 14 September 2011 (UTC)[reply]