Create a regular expression to match any of the phrases added to the trie (inspired by Dan Kogai's Regexp::Trie Perl module.
npm install regex-trie
requireand use (see the Usage section for more comprehensive usage instructions.)
var RegexTrie =trie =regex = trie;
npm install regex-trie
// app.jsvar RegexTrie =trie =regex = trie;console;
browserify app.js -o bundle.js.
var RegexTrie = ;// Create a new RegexTrie instancevar trie = ;// Add phrases to the trietrie;// You can use an array to add phrases if you'd rather (duplicate// pharses are ignored.)trie;// Fetch a RegExp to represent all the phrases in the trievar regex = trie; // regex => /(?:foo|ba[rz])/// What matches?var things_to_match = 'foo' 'bar' 'baz' 'bat' 'fun' 'food'match_results = things_to_match;console;// => [ true, true, true, false, false, true ]
Creates a new instance of
RegexTrie. Currently doesn't accept any options
however this will likely change as the module evolves.
Adds a new phrase to the trie. Accepts singleton arguments, or an array of phrases. Ignores any values which aren't literals (objects, bools, arrays, etc).
All numbers (except
NaN) are coerced into strings before being added.
Before adding new phrases, the trie is checked to see whether or not that
phrase already exists (using
Will check to see if the trie contains a phrase which matches
false if the phrase does or does not exist.
RegExp instance which should match each individual phrase in the
tree. The trie will escape any character that matches:
example, if the following values are added, the pipe (OR) will be escaped:
RegExp returned by
regex() is a non-capturing, un-anchored regular
expression meaning it'll never capture its matches and all of the following
phrases will still match:
var regex = trie;'fool' 'afool' 'bart' 'abart' 'acar' 'acard';// Output => true, true, true, true, true, true
regex-trie uses Gulp as its build system. Currently
gulpfile defines a few tasks:
.jshintrcfor this project's settings)
yuidocjsto produce development documentation
watch-- watches for changes to JS files in
./lib/and runs the
default-- by default the
watchtask runs (which runs
teston every JS file change.
package.json for the latest development dependencies. At the time
of writing, you'll need:
"mocha": "~1.17.1""should": "~3.1.2""gulp-jshint": "~1.4.0""gulp-util": "~2.2.14""gulp": "~3.5.2""gulp-watch": "~0.5.0""blanket": "~1.1.6""gulp-yuidoc": "~0.1.0"
List of things to add aren't in any specific order.
LICENSE.txt for license rights and limitations (MIT).