path-to-regex
Turn a path string such as /user/:id
or /user/:id(\d+)
into a regular expression
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');
let result = parser.match('/foo/asd');
let result = parser.match('/foo/123');
let result = parser.match('/foo/123/bar');
Demonstration of processing a key identifier with a specific content :keyname(\\d+)
let parser = new pathToRegex('/foo/:bar(\\d+)');
let result = parser.match('/foo/123');
let result = parser.match('/foo/asd');
let result = parser.match('/foo/123asd');
let result = parser.match('/foo/123/bar');
Demonstration of processing a multiple key identifiers :keyname1 ... :keyname2
let parser = new pathToRegex('/user/:foo/:bar');
let result = parser.match('/user/123/asd');
let result = parser.match('/user/asd/123');
Demonstration of processing a key identifiers with a repeated names :keyname(\\d+) ... :keyname(\d+)
let parser = new pathToRegex('/foo/:bar/:bar');
let result = parser.match('/foo/123/asd');
let result = parser.match('/foo/asd/123');
Demonstration of processing a key identifier with a quantifier ?
let parser = new pathToRegex('/foo/:bar?');
let result = parser.match('/foo/123');
let result = parser.match('/foo/');
let result = parser.match('/foo');
Demonstration of processing a key identifier with a quantifiers *
and +
let parser = new pathToRegex('/foo/:bar*');
let result = parser.match('/foo');
let result = parser.match('/foo/');
let result = parser.match('/foo/123');
let result = parser.match('/foo/123/456');
let result = parser.match('/foo/123/456/');
let parser = new pathToRegex('/foo/ids: :bar*/:count?');
let result = parser.match('/foo/ids: 123 456 789');
let result = parser.match('/foo/ids: 123 456 789/3');
let parser = new pathToRegex('/foo/:bar+');
let result = parser.match('/foo');
let result = parser.match('/foo/');
let result = parser.match('/foo/123');
let result = parser.match('/foo/123/456');
let result = parser.match('/foo/123/456/');
let parser = new pathToRegex('/foo/ids-,:bar+/:count?');
let result = parser.match('/foo/ids-123,456,789');
let result = parser.match('/foo/ids-123,456,789/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?');
let result = parser.match('/user/123/bar/111qwertyfak/222foo/test/pictures-p01.png,p02.png,p03.png');
let result = parser.match('/user/123/bar/111qwertyfak/222foo/test/pictures-p01.png,p02.png,p03.png/333');
let parser = new pathToRegex('/user/:id/bar/:key(\\d+):post?fak/:key(\d+)*:foo+/test/pictures- :multi(\w+?\.png)*/:key*');
let result = parser.match('/user/123/bar/111fak/222foo/test/pictures-p01.png p02.png p03.png');
let result = parser.match('/user/123/bar/111fak/222foo/test/pictures-p01.png p02.png p03.png/333');
let result = parser.match('/user/123/bar/111fak/222foo/test/pictures-p01.png p02.png p03.png/333/444/');
... documentation in processed