Try customized notations in JavaScript

In two simple steps.

Matching is implemented by a global function called match() taking two arguments: a value and a pattern. The value may be any JavaScript value of any predefined type or user-defined type. The pattern is a string used to recognize some form of data and decompose it into smaller pieces, and may contain pattern variables (prefixed by "%"), for isolating sub-data. A pattern may (currently) not contain any whitespace.

A failed match always returns the null value. A successful match returns an object having a field for each variable.

1. Match a value in the standard notation

A pattern in the standard notation is simply written in the native notation for data in JavaScript, sligthly extended to support variable-length arrays: See more examples.

Try it yourself:
Value =
Pattern =
Options: pattern variables


Note: the "unnamed" option means that pattern variables are anonymous, i.e. are represented by a single "%", not followed by any name. For unnamed patterns, the result of a match is an array having an element for each variable, in textual order.

2. Define your own notation for some datatype

You can very easily override the default notation above with a custom notation for any object type.

This is done by overriding the method "matches" (inherited from Object). The method matches(pat, off, acc) takes as arguments a pattern, given as a string plus an offset, and an accumulator, which is a partial result for the match. In case of a successful match, it has to return the new offset into the pattern, after the notation for this object has been consumed. In case of match failure, it has to throw a "fail" exception. The method is free to implement any kind of matching notation for the object, but which must always start with an opening parenthesis (one of "([{") and end with the corresponding closing parenthesis (one of ")]}"):


Now you can match any values containing your datatype, matched in your own notation:


Value =
Pattern =
Options: pattern variables