User talk:Gerbrant/edit/regexReplace.js
From Wikipedia, the free encyclopedia
- Gerbrant.edit.regexReplace
- This module provides a way to search for a regular expression, and selectively replace the matches.
Contents |
[edit] 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.
[edit] Installation
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" ]}} document.write('<script type="text/javascript" src="http://en.wikipedia.org/w/index.php?title=User:Gerbrant/fw.js&action=raw&ctype=text/javascript&dontcountme=s"></script>');
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.
[edit] Exports
Note: this module exports nothing when not editing an article.
- replace(what, ulcase, repl, isjs)
- Replace
what
withrepl
. Ifulcase == true
the search is case-sensitive. Ifisjs == 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.
[edit] Settings
- 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, not as a regex.
- caseSens
- If defined and true, the search is case-sensitive.
- replace
- A string or JavaScript-function for replacing matches.
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> ... } ... }