Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
path-to-regex
Advanced tools
Turn a path string such as /user/:id or /user/:id(\d+) into a regular expression
Turn a path string such as
/user/:id
or/user/:id(\d+)
into a regular expression
npm install path-to-regex --save
var pathToRegex = require('path-to-regex');
var parser = new pathToRegex(path_template, options?);
true
the regexp will be case sensitive. (default: true
)'/'
)true
the regexp will match from the beginning of the string. (default: true
)true
the regexp will match to the end of the string. (default: true
)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.
*
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' ] }
(...)
, 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' }
The following examples clearly demonstrate the use of keys, their pattern quantifiers.
: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
: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' }
: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' ] }
?
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 }
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
https://github.com/lastuniverse/path-to-regex/issues
MIT
FAQs
Turn a path string such as /user/:id or /user/:id(\d+) into a regular expression
The npm package path-to-regex receives a total of 2,067 weekly downloads. As such, path-to-regex popularity was classified as popular.
We found that path-to-regex demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.