Skip to content

url-pattern is easy pattern matching and segment extraction for urls, domains, filepaths and any string composed of segments joined by a separator character

License

Notifications You must be signed in to change notification settings

maddie927/url-pattern

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

url-pattern

NPM Package Build Status Dependencies

url-pattern is easy pattern matching and segment extraction for urls, domains, filepaths and any string composed of segments joined by a separator character

check out passage if you are looking for simple composable routing that builds on top of url-pattern

npm install url-pattern 

require with commonjs:

var Pattern = require('url-pattern');

lib/url-pattern.js can be used in the browser. it supports AMD as well.

match urls or filepaths

make pattern from string
var pattern = new Pattern('/users/:id');

the default separator is /. you can pass a custom separator as the second argument.

match pattern against url

match returns the extracted parameters or null if there was no match:

pattern.match('/users/5'); // => {id: '5'} pattern.match('/projects/5'); // => null
make pattern from regex
var regexPattern = new Pattern(/\/test\/(.*)/);
match regex pattern against url

if the pattern was created from a regex an array of the captured groups is returned on match:

regexPattern.match('/test/users'); // => ['users'] regexPattern.match('/users/test'); // => null
make wildcard pattern from string
var wildcardPattern = new Pattern('*/users/:id/*');
match wildcard pattern against url

wildcard matches are collected in the _ property:

wildcardPattern.match('/api/v1/users/10/followers/20'); // => {id: '10', _: ['/api/v1', 'followers/20']}
make optional pattern from string
var optionalPattern = new Pattern('(/)users(/:foo)/bar(/*)');
match optional pattern against url

optional matches are stored in the corresponding property, if they exist.

optionalPattern.match('users/bar'); // => {} optionalPattern.match('/users/bar'); // => {} optionalPattern.match('/users/biff/bar'); // => {foo: 'biff'} optionalPattern.match('/users/biff/bar/beep/boop'); // => {foo: 'biff', _: ['beep/boop']}

match domains

make pattern from string
var pattern = new Pattern(':sub.google.com', '.');

the default separator is /. you can pass a custom separator as the second argument to Pattern.

match pattern against domain

match returns the extracted parameters or null if there was no match:

pattern.match('www.google.com'); // => {sub: 'www'} pattern.match('www.google.io'); // => null
make pattern from regex
var regexPattern = new Pattern(/example\.(.*)/);
match regex pattern against domain

if the pattern was created from a regex an array of the captured groups is returned on match:

regexPattern.match('example.com'); // => ['com'] regexPattern.match('google.com'); // => null
make wildcard pattern from string
var wildcardPattern = new Pattern('*.:sub.google.*');
match wildcard pattern against url

wildcard matches are collected in the _ property:

wildcardPattern.match('subsub.www.google.com'); // => {sub: 'www', _: ['subsub', 'com']}

changelog

0.7

instead of

var urlPattern = require('url-pattern'); var pattern = urlPattern.newPattern('/example');

now use

var Pattern = require('url-pattern'); var pattern = new Pattern('/example');

About

url-pattern is easy pattern matching and segment extraction for urls, domains, filepaths and any string composed of segments joined by a separator character

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • CoffeeScript 100.0%