0.2.4 • Published 2 months ago

con-reg-exp v0.2.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

Convenient Regular Expressions

Web Page Tutorial Docs Web Demo 

!WARNING This is a project in an early stage of development. The main functionality is done. More work is needed especially in the context of documentation and tests.

Regular expression syntax redefined

The "Convenient Regular Expressions" give an alternative syntax for regular expressions. The main goal is to introduce a syntax that is easily manageable in complex regular expressions.

The module is still using a standard JavaScript's RegExp object. Essentially, it is a runtime transpiler that converts "Convenient Regular Expression" into a classic regular expression.

Usage

  1. Install with npm:

    npm install con-reg-exp
  2. Import it:

    import cre from "con-reg-exp";
  3. Use it as a tagged template:

    const myRegExp = cre`"Write your expression here."`;

You can find more details on usage in the documentation.

If you want to start using it, go to the tutorial.

Benefits

  • You can use whitespaces

    Organize your expression structure with spaces, new lines, and indentation.

  • You can use comments

    A good comment can clarify an expression a lot making it easier to understand later.

  • You are using words

    The words are significantly more readable than some arbitrarily selected special characters.

  • Maintenance is simpler

    If you want to make a change in a well-structured expression, you simply do it. With complex classic regular expressions, first, you have to parse it in your head, track brackets, and groups, and divide it into pieces.

  • Code review is faster and more accurate

    It is easier to understand well-structured expressions and changes in such expressions. Have you ever tried to review changes in regular expressions that are longer than 100 characters?

  • You can reuse the expressions

    You can put pieces of your expression into variables and reuse them multiple times later. It is like splitting your code into functions.

  • You are using sane syntax

    Long and complex regular expressions look like some esoteric programming languages created just to make the code unreadable.

Some example

Let's play a game. You have two functions. Try to understand what they are doing.

Classic RegExp implementation:

const pattern = /^\s*\[\s*(?:(?:-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?\s*,\s*)*-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?\s*)?\]\s*$/;

function guessWhatDoesThisFunctionDo(text) {
    return pattern.test(text);
}

Convenient Regular Expressions:

import cre from "con-reg-exp";

const number = cre`
    optional "-";               // Sign
    {                           // Integral part
        "0";                    // Zero is special case
    } or {
        [1-9], repeat digit;    // Everything above zero
    }
    optional {                  // Optional factional part
        ".";
        at-least-1 digit;
    }
    optional {                  // Optional exponent part
        [eE];
        optional [+-];
        at-least-1 digit;
    }
`;

const ws = cre`repeat whitespace`;

const pattern = cre`
    begin-of-text, ${ws};      // Trim leading whitespaces
    "[", ${ws};                // Begin of array
    optional {                 // Optional, because array can be empty
        repeat {               // Numbers with trailing comma
            ${number}, ${ws};
            ",", ${ws};
        }
        ${number}, ${ws};      // Last number has no comma
    }
    "]", ${ws};                // End of array
    end-of-text;
`;

function guessWhatDoesThisFunctionDo(text) {
    return pattern.test(text);
}

Which one was easier to understand? I'm assuming that you don't know the convenient regular expressions syntax yet. Were you able to guess despite this?

Click the link to see the answer.

Imagine that you want to modify the function to allow strings in the array.

0.2.4

2 months ago

0.2.3

2 months ago

0.2.2

3 months ago

0.2.1

3 months ago

0.2.0

3 months ago

0.1.0

3 months ago