[pmwiki-users] text replace using regex in edit textarea
Hans Bracker
design at softflow.uk
Fri Jan 23 10:23:40 PST 2026
thank you Petko, you are genius! It works just fine!
I am using a different regex though, from what you proposed, in order to
preserve empty lines between paragraphs, when selecting multiple paragraphs:
.replace(/([^\s])\n([^\s])/g, "$1 $2")
Hans
On 23/01/2026 17:36, Petko Yotov wrote:
> I would reuse the core JavaScript GUIEdit functions and the PmLib
> helpers available since PmWiki 2.4.0 (latest recommended).
>
> Create a file, say pub/myrxreplace.js, and put in it something like this:
>
> PmLib.ready(function(){
> const { dqs, dce, adjbe, tap } = PmLib; // import helpers
>
> const container = dqs('span.GUIButtons');
> if(!container) return;
> const button = dce('input', {type:'button', value:'RXR'});
> adjbe(container, button); // add before the end of GUIButtons
>
> function replace_in_selection(selection) {
> if(selection==='') return '';
> let result = selection
> .replace(/\r\n?/g, "\n") // normalize line breaks
> .replace(/ *(\n *)+/g, ' ') // join lines
> .replace(/pmwiki/ig, 'PmWiki') // fix capitalization
> // add your own
> ;
> return result;
> }
>
> tap(button, function(){ // onclick
> insMarkup(replace_in_selection);
> });
> });
>
> Enable this script in local/config.php, assumes $EnableGUIButtons is
> enabled:
>
> if($action=='edit') $HTMLHeaderFmt['myrxreplace']
> = '<script src="$PubDirUrl/myrxreplace.js"></script>';
>
> The "const button..." part is the button added to the end of the GUI
> buttons container. The value 'RXR' is the button label, obviously you
> can change it, or even use an emoji.
>
> Select some text in the edit textarea and click on the button. This
> will cause the function replace_in_selection() to be called with
> argument the current selection text. It should make the replacements
> and changes, then return the result which will be inserted in place of
> the selection in the edit textarea.
>
> I would also enable $EnablePreviewChanges = 1; and press "Preview" to
> see my changes before saving the page.
>
> Using the built-in helper functions is much simpler and easier than
> doing it in plain JavaScript, you don't need to locate the textarea,
> detect the selection, handle undo/redo, sync with PmSyntax, everything
> works under the hood.
>
> Petko
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.pmichaud.com/pipermail/pmwiki-users/attachments/20260123/6b7689e8/attachment.html>
More information about the pmwiki-users
mailing list