foo123
RegexAnalyzer
JavaScript

Regular Expression Analyzer and Composer for JavaScript, PHP, Python

Last updated Jun 23, 2026
98
Stars
12
Forks
1
Issues
0
Stars/day
Attention Score
36
Language breakdown
JavaScript 36.4%
PHP 35.9%
Python 27.7%
โ–ธ Files click to expand
README

Regex.Analyzer and Regex.Composer ================================= A generic, simple & intuitive Regular Expression Analyzer & Composer for PHP, Python, Javascript Regex v.1.2.0 (js only) These were used mostly as parts of other projects but uploaded here as standalone. See /test/js/test.js under /test folder for examples of how to use Regex.Analyzer Live Playground Example: Live Playground Example Regex.Composer Live Playground Example: Live Playground Example Example: (see /test/js/test.js)

//
 // use as: node test.js "yourregexhere" > output.txt
 "use strict";
 
 var echo = console.log;
 
 
 var Regex = require('../../src/js/Regex.js');
 
 echo("Regex.VERSION = " + Regex.VERSION);
 
 echo("Testing Regex.Composer");
 echo("===============================================================");
 
 var identifierSubRegex = Regex.Composer( )
 
                 .characterGroup( )
                     .characters( '_' )
                     .range( 'a', 'z' )
                 .end( )
 
                 .characterGroup( )
                     .characters( '_' )
                     .range( 'a', 'z' )
                     .range( '0', '9' )
                 .end( ).zeroOrMore( )
 
                 .partial( );
 
 var outregex = Regex.Composer( )
 
                 .SOL( )
 
                 .nonCaptureGroup( ).either( )
                     .regexp( identifierSubRegex )
                     .namedGroup( 'token' ).literal( 'aabb' ).end( )
                     .any( )
                     .space( )
                     .digit( false ).oneOrMore( )
                 .end( 2 ).zeroOrMore( false )
 
                 .backReference( 'token' )
 
                 .EOL( )
 
                 .compose( 'i' );
 
 echo("Partial        : " + identifierSubRegex);
 echo("Composed       : " + outregex.pattern.toString());
 echo("Expected       : " + "/^(?:[a-z][a-z0-9]|(\\\\aabb\\\\)|.|\\s|\\D+)?\\1$/i");
 echo("Output         : " + JSON.stringify(outregex, null, 4));
 echo("===============================================================");
 echo();
 
 var anal, peekChars, sampleStr, minLen, maxLen, groups, regexp,
     inregex = "/(?P<namedgroup>[abcde]+)fgh(?P=namedgroup)(?# a comment)/i";
 
 echo("Testing Regex.Analyzer");
 echo("===============================================================");
 
 // test it
 anal = Regex.Analyzer( inregex );
 peekChars = anal.peek( );
 minLen = anal.minimum( );
 maxLen = anal.maximum( );
 regexp = anal.compile( {i:anal.fl.i?1:0} );
 sampleStr = anal.sample( 1, 5 );
 groups = anal.groups();
 for(var i=0; i<5; i++)
 {
     var m = sampleStr[i].match(regexp);
     sampleStr[i] = {sample:sampleStr[i], match:(m ? 'yes' : 'no'), groups: {}};
     if ( m )
     {
         for(var g in groups)
             if ( Object.prototype.hasOwnProperty.call(groups,g) )
                 sampleStr[i].groups[g] = m[groups[g]];
     }
 }
 
 echo("Input                                       : " + inregex.toString( ));
 echo("Regular Expression                          : " + anal.input());
 echo("Regular Expression Flags                    : " + Object.keys(anal.fl).join(','));
 echo("Reconstructed Regular Expression            : " + anal.source());
 echo("===============================================================");
 echo("Regular Expression Syntax Tree              : ");
 echo(JSON.stringify(anal.tree(true), null, 4));
 echo("===============================================================");
 echo("Regular Expression (Named) Matched Groups   : ");
 echo(JSON.stringify(groups, null, 4));
 echo("===============================================================");
 echo("Regular Expression Peek Characters          : ");
 echo(JSON.stringify({positive:Object.keys(peekChars.positive||{}),negative:Object.keys(peekChars.negative||{})}, null, 4));
 echo("===============================================================");
 echo("Regular Expression Minimum / Maximum Length : ");
 echo(JSON.stringify({minimum:minLen,maximum:-1===maxLen?'unlimited':maxLen}, null, 4));
 echo("===============================================================");
 echo("Regular Expression Sample Match Strings     : ");
 echo(JSON.stringify(sampleStr, null, 4));
 echo("===============================================================");
Result
Regex.VERSION = 1.0.0
 Testing Regex.Composer
 ===============================================================
 Partial        : [a-z][a-z0-9]*
 Composed       : /^(?:[a-z][a-z0-9]|(\\aabb\\)|.|\s|\D+)?\1$/i
 Expected       : /^(?:[a-z][a-z0-9]|(\\aabb\\)|.|\s|\D+)?\1$/i
 Output         : {
     "source": "^(?:[a-z][a-z0-9]|(\\\\aabb\\\\)|.|\\s|\\D+)?\\1$",
     "flags": "i",
     "groups": {
         "1": 1,
         "token": 1
     },
     "pattern": {}
 }
 ===============================================================
 
 Testing Regex.Analyzer
 ===============================================================
 Input                                       : /(?P<namedgroup>[abcde]+)fgh(?P=namedgroup)(?# a comment)/i
 Regular Expression                          : (?P<namedgroup>[abcde]+)fgh(?P=namedgroup)(?# a comment)
 Regular Expression Flags                    : i
 Reconstructed Regular Expression            : ([abcde]+)fgh\1
 ===============================================================
 Regular Expression Syntax Tree              :
 {
     "type": "Sequence",
     "value": [
         {
             "type": "Group",
             "value": {
                 "type": "Sequence",
                 "value": [
                     {
                         "type": "Quantifier",
                         "value": {
                             "type": "CharacterGroup",
                             "value": [
                                 {
                                     "type": "Characters",
                                     "value": [
                                         "a",
                                         "b",
                                         "c",
                                         "d",
                                         "e"
                                     ]
                                 }
                             ]
                         },
                         "flags": {
                             "MatchOneOrMore": 1,
                             "min": 1,
                             "max": -1,
                             "isGreedy": 1
                         }
                     }
                 ]
             },
             "flags": {
                 "NamedGroup": 1,
                 "GroupName": "named_group",
                 "GroupIndex": 1
             }
         },
         {
             "type": "String",
             "value": "fgh"
         },
         {
             "type": "Special",
             "value": "1",
             "flags": {
                 "BackReference": 1,
                 "GroupName": "named_group",
                 "GroupIndex": 1
             }
         },
         {
             "type": "Comment",
             "value": " a comment"
         }
     ]
 }
 ===============================================================
 Regular Expression (Named) Matched Groups   :
 {
     "1": 1,
     "named_group": 1
 }
 ===============================================================
 Regular Expression Peek Characters          :
 {
     "positive": [
         "a",
         "b",
         "c",
         "d",
         "e",
         "A",
         "B",
         "C",
         "D",
         "E"
     ],
     "negative": []
 }
 ===============================================================
 Regular Expression Minimum / Maximum Length :
 {
     "minimum": 5,
     "maximum": "unlimited"
 }
 ===============================================================
 Regular Expression Sample Match Strings     :
 [
     {
         "sample": "AdbFGHAdb",
         "match": "yes",
         "groups": {
             "1": "Adb",
             "named_group": "Adb"
         }
     },
     {
         "sample": "CDfghCD",
         "match": "yes",
         "groups": {
             "1": "CD",
             "named_group": "CD"
         }
     },
     {
         "sample": "CBCfghCBC",
         "match": "yes",
         "groups": {
             "1": "CBC",
             "named_group": "CBC"
         }
     },
     {
         "sample": "BbaAFGHBbaA",
         "match": "yes",
         "groups": {
             "1": "BbaA",
             "named_group": "BbaA"
         }
     },
     {
         "sample": "EfghE",
         "match": "yes",
         "groups": {
             "1": "E",
             "named_group": "E"
         }
     }
 ]
 ===============================================================
see also:
  • Abacus Computer Algebra and Symbolic Computation System for Combinatorics and Algebraic Number Theory for JavaScript and Python
  • SciLite Scientific Computing Environment similar to Octave/Matlab/SciPy in pure JavaScript
  • TensorView view array data as multidimensional tensors of various shapes efficiently
  • FILTER.js video and image processing and computer vision Library in pure JavaScript (browser and nodejs)
  • HAAR.js image feature detection based on Haar Cascades in JavaScript (Viola-Jones-Lienhart et al Algorithm)
  • HAARPHP image feature detection based on Haar Cascades in PHP (Viola-Jones-Lienhart et al Algorithm)
  • MOD3 3D Modifier Library in JavaScript
  • Geometrize Computational Geometry and Rendering Library for JavaScript
  • Fuzzion a library of fuzzy / approximate string metrics for PHP, JavaScript, Python
  • Matchy a library of string matching algorithms for PHP, JavaScript, Python
  • Xpresion a simple and flexible eXpression parser engine (with custom functions and variables support), based on GrammarTemplate, for PHP, JavaScript, Python
  • codemirror-grammar transform a formal grammar in JSON format into a syntax-highlight parser for CodeMirror editor
  • ace-grammar transform a formal grammar in JSON format into a syntax-highlight parser for ACE editor
  • prism-grammar transform a formal grammar in JSON format into a syntax-highlighter for Prism code highlighter
  • highlightjs-grammar transform a formal grammar in JSON format into a syntax-highlight mode for Highlight.js code highlighter
  • syntaxhighlighter-grammar transform a formal grammar in JSON format to a highlight brush for SyntaxHighlighter code highlighter

ยฉ 2026 GitRepoTrend ยท foo123/RegexAnalyzer ยท Updated daily from GitHub