Skip to content

mheap/regex-rules

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 

regex-rules

This package allows you to specify a list of regexes and test inputs against them.

Build Status

Installation

npm install regex-rules --save

Usage

const RegexRules = require("regex-rules");
const r = new RegexRules(
  {
    empty: "^$",
    "more-than-20-chars": "^.{20,}$",
    "contains-link": "http://"
  },
  {
    "empty-or-long": ["empty", "more-than-20-chars"],
    "long-link": [["contains-link", "more-than-20-chars"]]
  }
);

// You can run individual rules. This can be useful to get a true/false result

// By default, anything provided is an OR e.g.
// The input is empty, or more than 20 characters long
r.evaluateRule("", "empty-or-long")); # true
r.evaluateRule("this-is-really-really-really-long", "empty-or-long")); # true
r.evaluateRule("foo", "empty-or-long")); # false

// If you need an AND, add another array e.g.
// Contains a link AND it's more than 20 characters
r.evaluateRule("http://example-is-long.com", "long-link")); # true
r.evaluateRule("http://foo.com", "long-link"); # false

// You can negate the result too
r.evaluateRule("http://foo.com", "!long-link"); # true

// Alternatively, you can run against all defined rules
r.run("this-is-really-really-really-long");
# {"empty-or-long": true, "long-link": false}
r.run("http://example-is-long.com");
# {"empty-or-long": true, "long-link": true}

Config

You can make regex matches case insensitive

const RegexRules = require("regex-rules");
const r = new RegexRules(
  {
    "example-domain: "^http://example\\.com",
  },
  {
    "insecure-example-com": ["example-domain"],
  },
  {
    case_insensitive: true
  }

  r.evaluateRule("http://EXAMPLE.com", "insecure-example-com"); # true
);

If you don't need to reuse regular expressions, you can provide the regex directly in the rule definition rather than referencing a regex by name:

const RegexRules = require("regex-rules");
const r = new RegexRules(
  {},
  {
    "insecure-example-com": ["^http://example\\.com"],
  },
  {
        allow_direct_regex: true
  }

  r.evaluateRule("http://EXAMPLE.com", "insecure-example-com"); # true
);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published