Wikisource:Regex
This page is being considered for deletion at Proposed deletions. Please see the discussion there. Start discussion Note for patrollers: if this page matches one of the criteria for speedy deletion, use {{sdelete}} instead. |
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).
- You must have a user account to use this functionality; if you don't have one, you can create one.
- 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).
- 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)."
- If you block javascript in your browser, you must enable third-party javascript (and css) from tools-static.wmflabs.org.
- 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.
- 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.
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 |