REDA

REDA : Regular Expression Development Aid. Converts annotated regular expressions introduced as inline scripts of type 'text/reda' into their syntactically correct form and re-inserts the updated script text with type '/text/javascript' back into the document.

by Hans PUFAL

HTML

<p>Annotated regular expresions are introduced as inline scripts of type '<code>text/jsREda</code>'. They are ignored by the browser, other than to include them in the DOM. The REda.js script processes each of these '<code>text/jsREda</code>' scripts, converting all annotated regular expressions into their inline form. Note no syntax checks are performd on the resulting regular expression.</p>

<p>An RE in a '<code>text/jsREda</code>' script is delimited by lines beginning with optional whitespace followed by a '/' character. The starting line may have an optional '//' comment following the opening '/'. This comment is used to identify the RE in the log (see below). The ending line of an annotated RE may have any JS text following it.</p>

<p>Inside an annotated RE, all '//' comments are removed as are all leading and trailing whitespace and newline characters. The inline RE is then assembled by joining the remaining pieces.</p>

<p>Once all of the annotated RE's are converted, the <code>text/jsREda</code> script is removed and the modified version is reinserted with a regular '<code>test/javascript</code>' type. This replacement triggers the browser to execute the script so any test code for the annotated RE's can be incorporated into that script. In the example below we simply <code>alert ("Welcode")</code> and log one of the annotated RE's tot he JavaScript console. (Note: <code>replaceChild</code>, though it replaces the script content and type fails to trigger the browser to execute the replacement).</p>
        
<p>A log of all the converted regular expressions is appended to the document body. Once development and testing is completed they can be copy/pasted into a regular JavaScript file.</p>

<p>The major limitation of this method is that it works only with inline scripts since browsers deny access to the source content of external scripts.</p>

<script type="text/jsREda">
  var REda =
        /                              //      REda main regexp
     ...

JavaScript

(function (doc) { 'use strict';
  // For example and to try : 
  for (var reda
         , preda
         , log = doc.createElement ('pre')
         , logtxt = ['<hr/>REDA log\n']
         , redas = doc.getElementsByTagName ('script')
         , r = redas.length; r--;)
    if ((reda = redas[r]).type === 'text/jsREda') {
      (preda = reda.parentNode).removeChild (reda);
      reda.innerHTML = reda.innerHTML
          .replace (/\/\s*(?:\/\/\s*(.*?))?\n((?:.|\n|\r)*?)\n\s*\/([img]*)/g,
            function (_, id, bdy, flgs) {
              bdy = bdy.split (/(?:\s*(?:\/\/[^\n\r]*)?(?:\r*\n\r*|\r)\s*)/)
                       .join ('')
                       .trim ();
              logtxt.push ((id ? id + ' : ' : '') + (bdy = '/' + bdy + '/' + flgs));
              return bdy;
            });
      reda.type = 'text/javascript';
      preda.appendChild (reda);
    };

  (log).innerHTML = logtxt.join ('\n');
  doc.body.appendChild (log);
}) (document);