Wikisource:Regex

From Wikisource
Jump to navigation Jump to search
Regex

Regexes can be very useful when editing. You can get regexes in your editing window, and save regex scripts you can run repeatedly with a single button press (for instance, you can make a button that removes the newlines around any single-character line).

  1. You must have a user account to use this functionality; if you don't have one, you can create one.
  2. Then add to your common.js file on the Meta wiki per the instructions at Meta:TemplateScript (note that this will give you regex functionality on any wiki you use this user account on, not just Wikisource).
  3. You must also enable it under the "Gadgets" tab in Special:Preferences: it's the tickbox next to "Add a 'regex editor' sidebar link which lets you write, apply, and save regex patterns (documentation)."
  4. If you block javascript in your browser, you must enable third-party javascript (and css) from tools-static.wmflabs.org.
  5. Go to a page window and click on the "edit" tab. In the side menu (location varies by wiki), under "Tools", you will find the heading "TemplateScript", and under that, a link that says "Regex editor". Click it.
  6. A regex window will appear, overlaying the editing pane.

Use examples

[edit]

A minimal regex example would be a straight find-replace, replacing "OLDTEXT" with "NEWTEXT". You would do this by entering "/OLDTEXT/g" (without the quotes) in the search box, and "NEWTEXT" in the replace box.

If you want to do this on another page, click "Save" to save the script to a button. A pop-up will ask you to name the button. After opening the edit tab on the other page, click on the "Regex editor" link as above, then click on the button you named.

Example recipes for Wikisource
Purpose Search string Replacement String Effect
Simple find-replace /oldtext/g newtext oldtext → newtext
Find-replace with special characters /\{\{c/g {{center block {{c|text}} → {{centerblock|text}}
Replace all ſes with ses. /\{\{ls\}\}/g s {{ls}} → s
Replace all lower-case ses, if they are followed by a lower-case letter, with ſes /s(?=[a-z])/g {{ls}} shyness → {{ls}}hyne{{ls}}s

Further reading

[edit]