Socket
Book a DemoInstallSign in
Socket

exceptional-expressions

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

exceptional-expressions

An incredible way to build efficient, concise and human readable regular expressions.

latest
Source
npmnpm
Version
0.2.3
Version published
Maintainers
1
Created
Source

exceptional expressions

Regex Expression Builder

dependency travis npm codecov

An incredible way to build efficient, concise and human readable regular expressions.
Made with ❤️ by ohitslaurence


Installing

Using yarn:

$ yarn add exceptional-expressions

Using npm:

$ npm install exceptional-expressions

Using bower:

$ bower install exceptional-expressions

Hello World

import { ExpBuilder, or, Constants, anythingBut } from 'exceptional-expressions;

const builder = new ExpBuilder('ig');

builder
  .beginsWith(or(['hello', 'goodbye']))
  .followedBy(Constants.whitespace)
  .endsWith(anythingBut('world'));

builder.matchesString('Hello World'); // false
builder.matchesString('Goodbye Earth'); // true
builder.matchesString('hello world'); // false
builder.matchesString('helloearth'); // false

Examples

The exceptional expressions builder class exposes many methods for chaining expressions in various combinations. These methods, combined with the various utility functions provide extensive functionality such as named grouping, or and optional chaining, exclusions and many more.

// Optional chaining
import { ExpBuilder, or, Constants, Sequences } from 'exceptional-expressions;

const builder = new ExpBuilder('g');

builder
  .beginsWith('(a)')
  .orBeginsWith('(b)')
  .followedBy(Constants.whitespace)
  .followedBy(or([Constants.word, Sequences.numbers(3)]));

builder.matchesString('(a) Test');  // true
builder.matchesString('(b) word');  // true
builder.matchesString('(a)string'); // false
builder.matchesString('(a) 123');  // true

builder.getMatches('(a) first extra test string (b) second');
// ['(a) first']

builder.toRegex(); // /^(?:(?:\(a\))|(?:\(b\)))\s(?:(?:[A-Za-z']+\b)|(?:[\d]{3}))/g

Named groups allow you to group chunks of your expression and then extract that chunk by the name that you gave it. This functionality seeks to improve on the regex.exec(string) method, which requires you to keep careful track on the ordering of your regex capture groups in order to determine which array index your group will be extracted into.

// Named groups
import { ExpBuilder, group, Constants } from 'exceptional-expressions;

const builder = new ExpBuilder('g');

builder
  .contains(group([Constants.word, '.', Constants.word], 'username'))
  .followedBy('@')
  .followedBy(
    group([
      group(Constants.word, 'company'), '.', group(Constants.word, 'tld')
    ], 'domain')
  );

builder.getCaptureGroups();
// ['username', 'domain', 'company', 'tld']

builder.matchesString('test.person@example.com');
// true

builder.getMatchesWithGroups('test.person@example.com, another.guy@test.io')
/* [{
      match: 'test.person@example.com',
      groups:
         {
           username: 'test.person',
           domain: 'example.com',
           company: 'example',
           tld: 'com'
         }
     },
     {
       match: 'another.guy@test.io',
       groups:
         {
           username: 'another.guy',
           domain: 'test.io',
           company: 'test',
           tld: 'io'
         }
    }]
*/

builder.getMatchesByGroup('test.person@example.com, another.guy@test.io', 'company')
// ['example', 'test']

License

MIT

Keywords

typescript

FAQs

Package last updated on 27 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts