Match a string against a list of patterns
Last updated 4 years ago by watson .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install patterns 
SYNC missed versions from official npm registry.


Match a string against a list of patterns.

build status js-standard-style

The name of this module was previosuly match-patterns, but Pavel Lang have been generous to give me the patterns name on NPM. If you are looking for the previous module it have been renamed design-patterns.


npm install patterns

Example usage

Add patterns using the .add() function:

var patterns = require('patterns')()

patterns.add('mathias', 'foo') // a pattern can be a string
patterns.add(/(tom|thomas)/, 'bar') // or a RegExp object
patterns.add('anders', 'baz')

var match = patterns.match('thomas')

if (match) console.log(match.value) // outputs 'bar'

The module can also be seeded with an array of patterns if you don't care about a 2nd argument that you can supply to the .add() function:

var patterns = require('patterns')([

if (patterns.match('foobar')) {


patterns.add(pattern[, value])


  • pattern - The pattern as either a string or a RegExp object
  • value (optional) - Returned as part of the match object when calling the .match() function. Can be of any type

If the pattern is a string it will be matched using the murl module. If the pattern is a RegExp object it will be matched as is.



  • target - The string that should be matched against each pattern

Runs through each pattern in order and returns a match object for the first match. If no pattern matches null is returned.



  • target - The string that should be matched against each pattern

Runs through each pattern in order and returns an array of match objects. If no pattern matches, an empty array is returned.

Example match object

  pattern: '/Users/{name}', // the matched pattern (1st argument to `.add()` function)
  target: '/Users/watson',  // the target string
  params: {                 // the named parameters from the pattern
    name: 'watson'
  value: value,             // the value (2nd argument to `.add()` function)
  next: function () {...}   // function to skip this match and continue

Note that if the pattern is a RegExp object, params will be the result of the native <str>.match(<ptn>) function (an array).

The next-function

The function can be used to skip the found match and continue matching the string against the patterns:

patterns.add(/foo/, 1)
patterns.add(/baz/, 2)
patterns.add(/bar/, 3)

var values = []

var match = patterns.match('foobar')
while (match) {
  match =

console.log(values) // [1, 3]

Example: A complete HTTP router

In this example the patterns module is used as a simple but powerful HTTP route matcher:

var http = require('http')
var url = require('url')
var patterns = require('patterns')()

patterns.add('GET /foo', fn1)
patterns.add('GET /foo/{id}', fn2)
patterns.add('POST /foo/{id}', fn3)

http.createServer(function (req, res) {
  var path = url.parse(req.url).pathname
  var match = patterns.match(req.method + ' ' + path)

  if (!match) {

  var fn = match.value // expects the value to be a function
  req.params = match.params

  fn(req, res)



Current Tags

  • 1.0.3                                ...           latest (4 years ago)

3 Versions

  • 1.0.3                                ...           4 years ago
  • 1.0.2                                ...           6 years ago
  • 0.0.1                                ...           8 years ago
Maintainers (1)
Today 0
This Week 4
This Month 7
Last Day 1
Last Week 1
Last Month 10
Dependencies (1)
Dev Dependencies (2)

Copyright 2014 - 2016 © |