new ReMix(name, spec)
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.defaultOptions
-
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 identifierstring 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/specpair(s). A name is a simple identifier which should match/^\w+$/. Thespeccan 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 identifierString Registered identifier to be removed.
-
add(spec)
lib/remix.js, line 429 -
Add a specification to the current instance.
Name Type Description specReMix~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 namestring The name for this specification.
specReMix~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 forceboolean 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
/ysticky 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
/ybe haviour missing is the fact that^would match beginning of line in/ymode even if/mis 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 normalRegExpobjects (assuming default 'g' flag is set).Name Type Description stringstring 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 nameReMix~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 optReMix~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
lastIndexto start at for the nextexec()call.Name Type Description indexnumber 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
ReMixobject.Properties:
Name Type Description nameReMix~Name Adds to the current name for
rewhich affect all children names for matches composed into theReMix.specReMix~Spec Specification to fall under
namenamespace.Example
var re = new ReMix({name: "myname", spec: /regex/}); -
OptionsObject
-
Properties:
Name Type Description nsDelimiterstring Sets the delimiter used for setting the ReMix~Namespace of named children. Defaults to '.'.
defaultFlagsstring | 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 namespacestring optional The current namespace.
Example
var re = new ReMix(function () { return { foo: /foo/, bar: /bar/ }; }); -
SpecRegExp ReMix ReMix~SetupCallback ReMix~NamedRegExp ReMix~Template Array.<ReMix~Spec> ReMix~Pairs
-
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