Find file
Fetching contributors…
Cannot retrieve contributors at this time
140 lines (86 sloc) 3.23 KB


Regular Expression with expanded functionality for random string generation and corner case strings computation


Current implementation supports a subset of regular expression syntax


^, \b, \B, and $ are zero-width characters that mark text and word boundaries

some text matches ^\bsome\B\btext\B$


. is a named range representing any character

\d, \D, \s, \S, \w, and \W are named ranges representing subsets that include or exclude digit, whitespace, or word characters

[...] and [^...] are custom set ranges representing subsets that include or exclude indicated subranges (single value and named).

- connecting two single value subranges represents the minimal interval including both values

Any unmatched and escaped characters in regular expression represent single value ranges


(...) is a capturing regular expression group that can be treated as a single reusable element

(?:...) is a non-capturing regular expression group that can be treated as a single unique element


Escaped natural numbers represent captured group elements

(a|b|c) == \\1 matches a == a, b == b, and c == c


?, *, and + are quatifiers of the preceding elements representing at most one, any number, and at least one repetition

{n}, {n,}, and {n,m} are quantifiers of the preceding elements representing exactly n, at least n, and between n and m repetitions


var regex = require('vs-regex');


Underlying regular expression class that provides all the functional capabilities to the module

var Regex = require('vs-regex').Regex;


A new instance of Regex object can be created by using a module shortcut:

var r = regex.create('regular expression');

or simply by using new on the class:

var r = new Regex('regular expression');


Check if regular expression matches a string:

//                 (   space |   vowel  |        consonant       )*
var r = new Regex('(?:([\\s])|([aeiouy])|([bcdfghjklmnpqrstvwxz]))*');

console.log( r.match('123 some text 456') );
console.log( r.match('123456789') );


  index: 3,
  input: "123 some text 456",
  "0": [
    { value: " some text ", index: 3 }
  "1": [
    { value: " ", index: 3 },
    { value: " ", index: 8 },
    { value: " ", index: 13 }
  "2": [
    { value: "o", index: 5 },
    { value: "e", index: 7 },
    { value: "e", index: 10 }
  "3": [
    { value: "s", index: 4 },
    { value: "m", index: 6 },
    { value: "t", index: 9 },
    { value: "x", index: 11 },
    { value: "t", index: 12 }



Calculate the minimum and maximum length of the strings matching regular expression


Generate a random string that matches regular expression

borders (not implemented)

Compute a set of strings that represent corner cases for regular expression


During the times when you have no idea why a regular expression does not behave the way you expect, it can be helpful to take a peek at the regular expression parse tree.