Re: text replace using regex in edit textarea

Hans Bracker <[email protected]> Fri, 23 Jan 2026 18:23:40 +0000
Newsgroups gmane.comp.web.wiki.pmwiki.user
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--===============0258757955604466665==
Content-Type: multipart/alternative;
 boundary="------------LKtVM4G3G45eTIUDtGhJRqmO"
Content-Language: en-GB

This is a multi-part message in MIME format.
--------------LKtVM4G3G45eTIUDtGhJRqmO
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit

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
>
--------------LKtVM4G3G45eTIUDtGhJRqmO
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>thank you Petko, you are genius! It works just fine! <br>
      I am using a different regex though, from what you proposed, in
      order to preserve empty lines between paragraphs, when selecting
      multiple paragraphs:<br>
    </p>
    <div
style="color: #cccccc;background-color: #1f1f1f;font-family: Consolas, 'Courier New', monospace;font-weight: normal;font-size: 21.599999999999998px;line-height: 28px;white-space: pre;"><div><span
    style="color: #cccccc;">.</span><span style="color: #dcdcaa;">replace</span><span
    style="color: #cccccc;">(</span><span style="color: #d16969;">/</span><span
    style="color: #ce9178;">([^</span><span style="color: #d16969;">\s</span><span
    style="color: #ce9178;">])</span><span style="color: #d16969;">\n</span><span
    style="color: #ce9178;">([^</span><span style="color: #d16969;">\s</span><span
    style="color: #ce9178;">])</span><span style="color: #d16969;">/</span><span
    style="color: #569cd6;">g</span><span style="color: #cccccc;">, </span><span
    style="color: #ce9178;">"$1 $2"</span><span style="color: #cccccc;">)</span></div></div>
    <p>Hans</p>
    <div class="moz-cite-prefix">On 23/01/2026 17:36, Petko Yotov wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:[email protected]">I would reuse
      the core JavaScript GUIEdit functions and the PmLib helpers
      available since PmWiki 2.4.0 (latest recommended).
      <br>
      <br>
      Create a file, say pub/myrxreplace.js, and put in it something
      like this:
      <br>
      <br>
        PmLib.ready(function(){
      <br>
          const { dqs, dce, adjbe, tap } = PmLib; // import helpers
      <br>
      <br>
          const container = dqs('span.GUIButtons');
      <br>
          if(!container) return;
      <br>
          const button = dce('input', {type:'button', value:'RXR'});
      <br>
          adjbe(container, button); // add before the end of GUIButtons
      <br>
      <br>
          function replace_in_selection(selection) {
      <br>
            if(selection==='') return '';
      <br>
            let result = selection
      <br>
              .replace(/\r\n?/g, "\n") // normalize line breaks
      <br>
              .replace(/ *(\n *)+/g, ' ')    // join lines
      <br>
              .replace(/pmwiki/ig, 'PmWiki') // fix capitalization
      <br>
              // add your own
      <br>
            ;
      <br>
            return result;
      <br>
          }
      <br>
      <br>
          tap(button, function(){ // onclick
      <br>
            insMarkup(replace_in_selection);
      <br>
          });
      <br>
        });
      <br>
      <br>
      Enable this script in local/config.php, assumes $EnableGUIButtons
      is enabled:
      <br>
      <br>
        if($action=='edit') $HTMLHeaderFmt['myrxreplace']
      <br>
           = '&lt;script
      src="$PubDirUrl/myrxreplace.js"&gt;&lt;/script&gt;';
      <br>
      <br>
      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.
      <br>
      <br>
      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.
      <br>
      <br>
      I would also enable $EnablePreviewChanges = 1; and press "Preview"
      to see my changes before saving the page.
      <br>
      <br>
      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.
      <br>
      <br>
      Petko
      <br>
      <br>
    </blockquote>
  </body>
</html>

--------------LKtVM4G3G45eTIUDtGhJRqmO--


--===============0258757955604466665==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
pmwiki-users mailing list
[email protected]
http://www.pmichaud.com/mailman/listinfo/pmwiki-users

--===============0258757955604466665==--