Skip to content

Convenient Regular Expressions - a different approach to the regular expressions syntax.

License

Notifications You must be signed in to change notification settings

kildom/con-reg-exp

Repository files navigation

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.

About

Convenient Regular Expressions - a different approach to the regular expressions syntax.

Topics

Resources

License

Stars

Watchers

Forks