reglist

0.1.1 • Public • Published

Reglist, a Regular Expression List Compiler.

Take a list of regexes and make a single regex. Gives you which regex matched and its matching groups.

var Reglist = require('reglist')
var reglist = new Reglist()
reglist.add([
  /[^\-]*-([^\-])/,
  /data:([a-z\/]+)(?:;base64|;binary),.+/,
])
var matched = reglist.match("data:text/plain;base64,aDfwkTPmx98==")
// { index: 1,
//   match: [ 'data:text/plain;base64,aDfwkTPmx98==', 'text/plain' ] }

Speed

Using a reglist instead of a list of regular expressions was studied as the means by which the server framework matches the requests' URL to callbacks. On http://shields.io, the following measurement was made by loading the front page, in nanoseconds:

# time-no-reglist
avg: 42361.79518072289
median: 35912
# time-reglist
avg: 43857.22891566265
median: 32390

Reglist's match is about as fast as looping over a list of regexes. Reglist's test is a bit slower.

Interface

  • add: function(regexes: Array<RegExp>) appends a regex to the list.
  • push: function(regex: RegExp) appends just one regex.
  • match: function(string: String) checks whether the string matches one of the regexes in the list, and returns:
    • index: Number the index of the regex in the list,
    • match: Array<String> a list corresponding to the matching groups of the regex that matched.
  • test: function(string: String): Boolean checks whether the string matches one of the regexes in the list.
  • regexes: Array<RegExp> is the list of regexes. Do not change it.

Readme

Keywords

none

Package Sidebar

Install

npm i reglist

Weekly Downloads

0

Version

0.1.1

License

MIT

Last publish

Collaborators

  • espadrine