Socket
Socket
Sign inDemoInstall

path-to-regexp

Package Overview
Dependencies
0
Maintainers
1
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
49M
decreased by-0.54%
Maintainers
1
Install size
3.56 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

Changelog

Source

0.0.2 / 2013-02-10

  • Update to match current express
  • Add .license property to component.json

Readme

Source

Path-to-RegExp

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

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.
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 10 Feb 2013

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