npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

path-to-regex

v1.3.8

Published

Turn a path string such as /user/:id or /user/:id(\d+) into a regular expression

Downloads

10,990

Readme

path-to-regex

Turn a path string such as /user/:id or /user/:id(\d+) into a regular expression

NPM version Dependency Status License Downloads

Installation

npm install path-to-regex --save

Usage

var pathToRegex = require('path-to-regex');

var parser = new pathToRegex(path_template, options?);
  • path_template A string or a regular expression.
  • options
    • case When true the regexp will be case sensitive. (default: true)
    • separators The chars list for splited patch string. (default: '/')
    • fromStart When true the regexp will match from the beginning of the string. (default: true)
    • toEnd When true the regexp will match to the end of the string. (default: true)

How it works?

It is important to understand how the key :key is interpreted depending on the pattern :key(.*) used and quantifiers :key*. The following examples will help you understand the logic for obtaining key values.

the quantifier * will capture everything that is not a separator options.separators

let parser = new pathToRegex(':path*');              // parser.regexp:  /^[\/]?((?:[\/]?[^\/]+)*)[\/]?$/

let result = parser.match('user/id');                // result: { path: [ 'user', 'id' ] }
let result = parser.match('/user/id');               // result: { path: [ 'user', 'id' ] }
let result = parser.match('user/id/');               // result: { path: [ 'user', 'id' ] }
let result = parser.match('/user/id/');              // result: { path: [ 'user', 'id' ] }

let parser = new pathToRegex('/:path*');             // parser.regexp:  /^[\/]?((?:[\/]?[^\/]+)*)[\/]?$/

let result = parser.match('user/id');                // result: { path: [ 'user', 'id' ] }
let result = parser.match('/user/id');               // result: { path: [ 'user', 'id' ] }
let result = parser.match('user/id/');               // result: { path: [ 'user', 'id' ] }
let result = parser.match('/user/id/');              // result: { path: [ 'user', 'id' ] }

Pattern (...), in contrast quantifier, allows you to directly determine the valid key pattern. Such pattern (.*) will capture everything, including the splitter.

let parser = new pathToRegex(':path(.*)');           // parser.regexp:  /^[\/]?(.*?)[\/]?$/

let result = parser.match('user/id');                // result: { path: 'user/id' }
let result = parser.match('/user/id');               // result: { path: 'user/id' }
let result = parser.match('user/id/');               // result: { path: 'user/id' }
let result = parser.match('/user/id/');              // result: { path: 'user/id' }

let parser = new pathToRegex('/:path(.*)');          // parser.regexp:  /^[\/]?(.*?)[\/]?$/

let result = parser.match('user/id');                // result: { path: 'user/id' }
let result = parser.match('/user/id');               // result: { path: 'user/id' }
let result = parser.match('user/id/');               // result: { path: 'user/id' }
let result = parser.match('/user/id/');              // result: { path: 'user/id' }

Samples

The following examples clearly demonstrate the use of keys, their pattern quantifiers.

Demonstration of processing a key identifier with a specific content :keyname(\\d+)

let parser = new pathToRegex('/foo/:bar(\\d+)');            // parser.regexp:  /^[\/]?foo\/?(\d+?)[\/]?$/

let result = parser.match('/foo/123');                      // result: { bar: '123' }
let result = parser.match('/foo/asd');                      // result: undefined
let result = parser.match('/foo/123asd');                   // result: undefined
let result = parser.match('/foo/123/bar');                  // result: undefined

Demonstration of processing a multiple key identifiers :keyname1 ... :keyname2

let parser = new pathToRegex('/user/:foo/:bar');            // parser.regexp:  /^[\/]?user\/?([^\/]+?)\/?([^\/]+?)[\/]?$/

let result = parser.match('/user/123/asd');                 // result: { foo: '123', bar: 'asd' }
let result = parser.match('/user/asd/123');                 // result: { foo: 'asd', bar: '123' }

Demonstration of processing a key identifiers with a repeated names :keyname(\\d+) ... :keyname(\d+)

let parser = new pathToRegex('/foo/:bar/:bar');             // parser.regexp:  /^[\/]?foo\/?([^\/]+?)\/?([^\/]+?)[\/]?$/

let result = parser.match('/foo/123/asd');                  // result: { bar: [ '123', 'asd' ] }
let result = parser.match('/foo/asd/123');                  // result: { bar: [ 'asd', '123' ] }

Demonstration of processing a key identifier with a quantifier ?

let parser = new pathToRegex('/foo/:bar?');                 // parser.regexp:  /^[\/]?foo\/?([^\/]+?)?[\/]?$/

let result = parser.match('/foo/123');                      // result: { bar: '123' }
let result = parser.match('/foo/');                         // result: { bar: undefined }
let result = parser.match('/foo');                          // result: { bar: undefined }

Demonstration of processing a key identifier with all features

let parser = new pathToRegex('/user/:id/bar/:key(\\d+):post?fak/:key(\d+)*:foo+/test/pictures-:multi(\w+?\.png)*/:key?');               // parser.r/]+?)?fak\/((?:[^\/]*\d+)*)((?:[^\/]*[^\/]+)+)\/test\/pictures-((?:[^\/]*\w+?\.png)*)\/?([^\/]+?)?[\/]?$/

let result = parser.match('/user/123/bar/111qwertyfak/222foo/test/pictures-p01.png,p02.png,p03.png/333');
/* result: { id: '123',
   key: [ '111', '222', '333' ],
   post: 'qwerty',
   foo: [ 'foo' ],
   multi: [ 'p01.png', 'p02.png', 'p03.png' ] } */

let result = parser.match('/user/123/bar/111qwertyfak/222foo/test/pictures-p01.png-p02.png-p03.png');
/* result: { id: '123',
   key: [ '111', '222' ],
   post: 'qwerty',
   foo: [ 'foo' ],
   multi: [ 'p01.png', 'p02.png', 'p03.png' ] } */

let result = parser.match('/user/123/bar/111fak/222foo/test/pictures-p01.png,p02.png,p03.png');
/* result: { id: '123',
   key: [ '111', '222' ],
   post: undefined,
   foo: [ 'foo' ],
   multi: [ 'p01.png', 'p02.png', 'p03.png' ] } */

let result = parser.match('/user/123/bar/111fak/foo/test/pictures-p01.png;p02.png;p03.png');
/* result: { id: '123',
   key: [ '111' ],
   post: undefined,
   foo: [ 'foo' ],
   multi: [ 'p01.png', 'p02.png', 'p03.png' ] } */

let result = parser.match('/user/123/bar/111fak/foo/test/pictures-p01.png p02.png');
/* result: { id: '123',
   key: [ '111' ],
   post: undefined,
   foo: [ 'foo' ],
   multi: [ 'p01.png', 'p02.png' ] } */

let result = parser.match('/user/123/bar/111fak/foo/test/pictures-p01.png');
/* result: { id: '123',
   key: [ '111' ],
   post: undefined,
   foo: [ 'foo' ],
   multi: [ 'p01.png' ] } */

let result = parser.match('/user/123/bar/111fak/foo/test/pictures-');
/* result: { id: '123',
   key: [ '111' ],
   post: undefined,
   foo: [ 'foo' ],
   multi: [] } */

... documentation in processed

Tests

Tests

Participation in development

https://github.com/lastuniverse/path-to-regex/issues

License

MIT

NPM