User talk:Gerbrant/edit/regexReplace.js

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
Gerbrant.edit.regexReplace
This module provides a way to search for a regular expression, and selectively replace the matches.

Usage[edit]

When installed, this script inserts a textbox, a checkbox and a "Replace..."-button just before the main editing textbox when you edit an article. Enter a regular expression and click the "Replace..."-button to search for it. ^ and $ mean the start and the end of the article or section. If the checkbox is checked the search will be case-sensitive.

When the search completes, another textbox, another checkbox and a "Replace selection"-button is shown, followed by all the matches. The matches are shown bold and prefixed with a checkbox; only those matches that are checked will be replaced.

Enter a bit of text in the second textbox and click the "Replace selection"-button to replace all selected matches with the new text. If the checkbox is checked, the new text will be interpreted as a JavaScript-function and the matches will be replaced with whatever the function returns. The first parameter of this function will be the full match, and the remaining parameters will be all captured submatches.

Masking[edit]

You can use a JavaScript mask function. To do this, first enter the JavaScript function to use in the first textbox, then "**", and then the regular expression to search for. During the search this function will be passed the same arguments as the replacement function will be passed during replacement. When for a match the mask function returns true, a checkbox corresponding to the match will be displayed, and the user can decide whether replacement should occur, like usual. However, is false returned, then the match will not be replaced, and no checkbox will be displayed. If the mask function throws an error, this is reported to the user and a return value of true is assumed. Using a mask function differs from using a replacement function that returns its argument in that no checkbox is displayed for the match.

Installation[edit]

It is recommended to use this script in conjunction with Gerbrant.fw (documentation). If you're already using it, just add "Gerbrant.edit.regexReplace" to the start of the list of modules to load, otherwise paste the following in your monobook.js:

Gerbrant = {fw: {load: [

"Gerbrant.edit.regexReplace"

]}}

mw.loader.load("http://en.wikipedia.org/w/index.php?title=User:Gerbrant/fw.js&action=raw&ctype=text/javascript"); You can also load this module without it, although exports and functionality that depends on code from another module (except wikibits.js) will not be available.

Exports[edit]

Note: this module exports nothing when not editing an article.

replace(what, ulcase or mask, repl, isjs)
Replace what with repl. If ulcase == true the search is case-sensitive. If isjs == true, repl is interpreted as a JavaScript function. Behaviour is identical to filling in the relevant textboxes, checking the relevant checkboxes and clicking the "Replace..."-button.
If repl is a JavaScript function object and isjs is undefined, this object will be used as replacement function.
If what is a regular expression object then mask should be a JavaScript function object to be used as mask function, or null.

Settings[edit]

lang
String, language of the user interface. Can be "en" or "nl", usually derived from the language of Wikipedia's user interface. Defaults and falls back to "en".
presets
An Array of Objects, each a predefined set of values to use that will be added to the toolbox.
regex
A regular expression, as a string or as a regex. If you use a regex caseSens will be ignored; be sure to use the right matching flags on the regex itself - this includes g if you intend to find more than just the first match.
caseSens
If defined and true, the search is case-sensitive.
replace
A string or JavaScript-function for replacing matches.
mask
A mask function to use. Optional.

Example:

{
    lang: "nl", 
    presets:
    {
        Test:
        {
            regex: "[A-Z]{2,}",
            caseSens: true,
            replace: function(a)
            {
                return a.slice(0,1) +
                    a.slice(1).toLowerCase();
            }
        }
    }
}

This example can be tested by inserting it in your monobook.js:

Gerbrant = { ...
edit: { ...
regexReplace: <example>
... } ... }