User talk:Gerbrant/edit/regexReplace.js


 * Gerbrant.edit.regexReplace
 * This module provides a way to search for a regular expression, and selectively replace the matches.

Usage
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
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, a checkbox corresponding to the match will be displayed, and the user can decide whether replacement should occur, like usual. However, is  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  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.

You can also load this module without it, although exports and functionality that depends on code from another module (except ) will not be available.

Exports
Note: this module exports nothing when not editing an article.
 * replace(what, ulcase or mask, repl, isjs)
 * Replace  with  . If   the search is case-sensitive. If ,   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  is a JavaScript function object and   is undefined, this object will be used as replacement function.
 * If  is a regular expression object then   should be a JavaScript function object to be used as mask function, or.

Settings

 * lang
 * String, language of the user interface. Can be  or , usually derived from the language of Wikipedia's user interface. Defaults and falls back to.


 * 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  will be ignored; be sure to use the right matching flags on the regex itself - this includes   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  : Gerbrant = { ... edit: { ... regexReplace: ... } ... }