<!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:4dc9d064c1ebeb25335a066e6034bf24@5ko.fr">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>
           = '<script
      src="$PubDirUrl/myrxreplace.js"></script>';
      <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>