Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

path-to-regex

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

path-to-regex

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

  • 1.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.5K
decreased by-5.81%
Maintainers
1
Weekly downloads
 
Created
Source

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 matcher = 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)
    • splitters The chars list for splited patch string. (default: '/')
    • escapeChars The chars list for escaped. (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)

Samples

Demonstration of processing a simple key identifier :keyname
let parser = new pathToRegex('/foo/:bar');
// parser.regexp:  /^\/foo\/([^\/]+)[\/]?$/

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

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

let result = regex.match('/foo/123/bar');  // result: undefined
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 = regex.match('/foo/123');  // result: { bar: '123' }

let result = regex.match('/foo/asd');  // result: undefined

let result = regex.match('/foo/123asd');  // result: undefined

let result = regex.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 = regex.match('/user/123/asd');  // result: { foo: '123', bar: 'asd' }

let result = regex.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 = regex.match('/foo/123/asd');  // result: { bar: [ '123', 'asd' ] }

let result = regex.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 = regex.match('/foo/123');  // result: { bar: '123' }

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

let result = regex.match('/foo');  // result: { bar: undefined }
Demonstration of processing a key identifier with a quantifiers * and +
let parser = new pathToRegex('/foo/:bar*');
// parser.regexp:  /^\/foo\/?((?:\/[^\/]+)*)[\/]?$/

let result = regex.match('/foo');  // result: { bar: [] }

let result = regex.match('/foo/');  // result: { bar: [] }

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

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

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


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

let result = regex.match('/foo/ids: 123 456 789');  // result: { bar: [ '123 456 789' ], count: undefined }

let result = regex.match('/foo/ids: 123 456 789/3');  // result: { bar: [ '123 456 789' ], count: '3' }


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

let result = regex.match('/foo');  // result: undefined

let result = regex.match('/foo/');  // result: undefined

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

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

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


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

let result = regex.match('/foo/ids-123,456,789');  // result: { bar: [ '123,456,789' ], count: undefined }

let result = regex.match('/foo/ids-123,456,789/3');  // result: { bar: [ '123,456,789' ], count: '3' }
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.regexp:  /^\/user\/([^\/]+)\/bar\/(\d+)([^\/]+)?fak\/?((?:\d+\/?)*)?((?:[^\/]+\*?)+)\/test\/pictures-,?((?:\w+?\.png\,?)*)\/?([^\/]+)?[\/]?$/

let result = regex.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 = regex.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 parser = new pathToRegex('/user/:id/bar/:key(\\d+):post?fak/:key(\d+)*:foo+/test/pictures- :multi(\w+?\.png)*/:key*');
// parser.regexp:  /^\/user\/([^\/]+)\/bar\/(\d+)([^\/]+)?fak\/?((?:\d+\/?)*)?((?:[^\/]+\*?)+)\/test\/pictures- ?((?:\w+?\.png\ ?)*)\/?((?:\/[^\/]+)*)[\/]?$/

let result = regex.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 = regex.match('/user/123/bar/111fak/222foo/test/pictures-p01.png p02.png p03.png/333');
/* result:
 { id: '123',
  key: [ '111', '222', '333' ],
  post: undefined,
  foo: [ 'foo' ],
  multi: [ 'p01.png', 'p02.png', 'p03.png' ] } 
*/

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

... documentation in processed

NPM

Keywords

FAQs

Package last updated on 17 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc