Socket
Socket
Sign inDemoInstall

path-to-regexp

Package Overview
Dependencies
0
Maintainers
33
Versions
55
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    path-to-regexp

Express style path to RegExp utility


Version published
Weekly downloads
41M
decreased by-14.43%
Maintainers
33
Install size
7.16 kB
Created
Weekly downloads
 

Package description

What is path-to-regexp?

The path-to-regexp package is a utility for converting paths to and from regular expressions. It is commonly used for routing in web applications, allowing developers to define patterns for URL paths and extract parameters from them.

What are path-to-regexp's main functionalities?

Path to RegExp Conversion

Convert a path string into a regular expression. It can also extract named parameter keys.

const { pathToRegexp } = require('path-to-regexp');
const keys = [];
const regexp = pathToRegexp('/user/:id', keys);

Extracting Parameters from a Path

Match a path against a pattern and extract the named parameters.

const { match } = require('path-to-regexp');
const matchFn = match('/user/:id');
const result = matchFn('/user/123');
// result.params will contain the extracted parameters

Compile Path to String

Compile a path function from a string pattern, which can then be used to construct paths with parameters.

const { compile } = require('path-to-regexp');
const toPath = compile('/user/:id');
const path = toPath({ id: 123 });
// path will be '/user/123'

Other packages similar to path-to-regexp

Readme

Source

Path-to-RegExp

Turn an Express-style path string such as /user/:name into a regular expression.

Note: This is a legacy branch. You should upgrade to 1.x. If you find you are stuck on this version, your code will need to take into account the index property on the keys.

Usage

var pathToRegexp = require('path-to-regexp');

pathToRegexp(path, keys, options)

  • path A string in the express format, an array of such strings, or a regular expression
  • keys An array to be populated with the keys present in the url. Once the function completes, this will be an array of strings.
  • options
    • options.sensitive Defaults to false, set this to true to make routes case sensitive
    • options.strict Defaults to false, set this to true to make the trailing slash matter.
    • options.end Defaults to true, set this to false to only match the prefix of the URL.
var keys = [];
var exp = pathToRegexp('/foo/:bar', keys);
//keys = ['bar']
//exp = /^\/foo\/(?:([^\/]+?))\/?$/i

Live Demo

You can see a live demo of this library in use at express-route-tester.

License

MIT

Keywords

FAQs

Last updated on 19 Jun 2015

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc