Class: ReMix

ReMix

new ReMix(name, spec)

lib/remix.js, line 51

Construct a new ReMix object.

The name sets this ReMix's name. The name is used when returning matches. All sub-child matches are namespaced based on the hierarchy of names. name is optional.

Any arguments after name are passed to ReMix#add.

Name Type Description
name ReMix~Name optional

The name for this ReMix object.

spec ReMix~Spec

Specification for this ReMix object.

Example
var a1 = new ReMix('a1'),
    a2 = new ReMix('a2'),
   str = "foobar";

a1.options({nsDelimiter: '/'});
a1.add({foo: /foo/});
a2.add({bar: /bar/});

a1.add(a2);

console.log(a1.exec(str)); // [['foo'], 'a1/foo',    0, 3]
console.log(a1.exec(str)); // [['foo'], 'a1/a2/bar', 1, 6]

Members

staticReMix.defaultOptionsReMix~Options

Default options. Changing these affects all future objects.

Methods

staticReMix.getRegistered(identifier){RegExp}

lib/remix.js, line 269

Get the regular expression registered by identifier.

Name Type Description
identifier string

Name of registered template variable to return.

See:
Returns:
regular expression.
Example
var regex = ReMix.getRegistered('templateName');

staticReMix.register(arguments){ReMix}

lib/remix.js, line 231

Register a new match template variable for use in template match specs.

Name Type Description
arguments *

Arguments are in one of two formats. Both forms specify name/spec pair(s). A name is a simple identifier which should match /^\w+$/. The spec can be either a regular expression or or another template.

Example
ReMix.register('end', /$/)
     .register('eol', /(?:\r\n?|\n|\f)/);

ReMix.register({
  word:        /\w+/,
  space:       /\s+/,
  wordOrSpace: '{word}|{space}'
});

staticReMix.unregister(identifier){ReMix}

lib/remix.js, line 256

UnRegister a match template variable.

Name Type Description
identifier String

Registered identifier to be removed.

add(spec)

lib/remix.js, line 429

Add a specification to the current instance.

Name Type Description
spec ReMix~Spec repeatable

All arguments are flattened into a single specification.

addNamed(name, spec)

lib/remix.js, line 443

Add a named specification. This is synonymous with remix.add({name: name, spec: spec}).

Name Type Description
name string

The name for this specification.

spec ReMix~Spec repeatable

The remaining arguments are consumed as the spec.

asString(){String}

lib/remix.js, line 574

For debugging purposes, returns a string representation of our compiled regular expressions.

clear()

lib/remix.js, line 416

Clears all set specifications and compilations.

clone(){ReMix}

lib/remix.js, line 332

Returns a shallow clone.

compile(force){Array}

lib/remix.js, line 462

Compile this ReMix object. Should never need to call. Any method which needs to be compiled will call this.

Name Type Description
force boolean

force recompilation.

exec(string){ReMix~Match}

lib/remix.js, line 518

Compiles and executes our list of regular expressions on the given string. This method acts a little like the new /y sticky flag. If the match is offset into the string (the regex engine looked past the start of the string) it is considered rejected.

The only part of /y be haviour missing is the fact that ^ would match beginning of line in /y mode even if /m is not set. We do not go this far, it would require too much overhead for this library to be useful.

All internal RegExp objects are updated with the lastIndex of this match so that calling exec() again has the same effect it does on normal RegExp objects (assuming default 'g' flag is set).

Name Type Description
string string

The string to execute on

Example
var re = new ReMix('fb', { foo: /(fo)o/, bar: /ba(r)/ });
var str = "foobar", match;
                      // match           Namespace  Spec Idx  lastIndex
match = re.exec(str); // [['foo', 'fo'], 'fb.foo',  0,        3]
match = re.exec(str); // [['bar',  'r'], 'fb.bar',  1,        6]
match = re.exec(str); // false

hasSpecs(){boolean}

lib/remix.js, line 452

Returns if this instance has an specifications set.

name(name){ReMix~Name}

lib/remix.js, line 402

Sets the name for the current ReMix instance. If this instance has already been compiled, the compilation is reset and will happen the next time it's needed.

Name Type Description
name ReMix~Name optional

If specified, sets the current name.

Returns:
current name.
Example
var remix = new ReMix();
remix.name('s1');
remix.add(/foo/);

remix.exec("foo"); // [['foo'], 's1', 0, 3]

options(opt){ReMix~Options}

lib/remix.js, line 319

Set and retrieve current options.

Name Type Description
opt ReMix~Options optional

Merges with current options if specified.

Returns:
current options object.
Example
var remix = new ReMix();
remix.options({
  nsDelimiter:  '.', // default
  defaultFlags: 'g'  // default
});

setLastIndex(index)

lib/remix.js, line 489

Sets the lastIndex to start at for the next exec() call.

Name Type Description
index number

The index in your string to set lastIndex to.

Type Definitions

Namestring

The name being set. This can be anything but should not contain the ReMix~Options.nsDelimiter

Example
var re = new ReMix();
re.name("foo");
re.add({bar: /bar/});
re.add({baz: /baz/});
var str = "barbaz", match;

match = re.exec(str); // [["bar"], "foo.bar", 0, 3]
match = re.exec(str); // [["baz"], "foo.baz", 1, 6]

NamedRegExpObject

This is a simple way to specify a single named specification within the current without having to create a new ReMix object.

Properties:
Name Type Description
name ReMix~Name

Adds to the current name for re which affect all children names for matches composed into the ReMix.

spec ReMix~Spec

Specification to fall under name namespace.

Example
var re = new ReMix({name: "myname", spec: /regex/});

OptionsObject

Properties:
Name Type Description
nsDelimiter string

Sets the delimiter used for setting the ReMix~Namespace of named children. Defaults to '.'.

defaultFlags string | Array.<string>

Default flags for RegExp objects created. Defaults to 'g'.

PairsObject.<ReMix~Namespace, ReMix~Spec>

This is a simple way to create multiple sub named regular expressions without having to create a new ReMix object for each one.

Example
var re = new ReMix({
  name1: /foo/, // name1     -> /foo/
  name2: {
    bar: /bar/  // name2.bar -> /bar/
    baz: /baz/  // name2.baz -> /baz/
  }
});

RegisterMatchObjectObject.<string, ReMix~RegisterMatchSpec>

Example
ReMix.register({
  cMultiComment: /\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\//,
  cLineComment:  "//{notEol*}{eol}"
});

RegisterMatchSpecRegExp ReMix~Template

Example
ReMix.register('eol',        /(?:\r\n?|\n|\f)/);
ReMix.register('emptyLines', '{eol}{eol+}');

SetupCallback(namespace){ReMix~Spec}

lib/remix.js, line 602

Setup callback. Called during ReMix compilation. Any rules created within this callback are parented by the current Rule.

Name Type Description
namespace string optional

The current namespace.

Example
var re = new ReMix(function () {
  return {
    foo: /foo/,
    bar: /bar/
  };
});
Example
var re = new ReMix();
re.add(/regexp/);        // RegExp
re.add(function () {     // ReMix~SetupCallback
  return {               // ReMix~NamedRegExp
    name: "foo"
    spec: [              // ReMix~Spec[]
      {                  // ReMix~Pairs
        baz: 'foo{eol}$' // ReMix~Template
      },
      function () {      // ReMix~SetupCallback
        return /bar/
      }
    ]
  };
});

Templatestring

This template is processed by ReMix#_resolveTemplate. Templates can contain variable which are delimited by {} and have an optional ?+* modifier.

Example
var re = new ReMix();
re.add("{word+}{hspace*}{eol?}$");

ReMix.register('foo', /foo/);
ReMix.register('fooOrBar', '{bar}|bar');

re.add("{fooOrBar?}{foo}\\s+");       // don't forget to escape backslash

re.add(/{fooOrBar?}{foo}\s+/.source); // alternativly, {} has no special meaning in
                                      // Regular expressions