
Security News
npm ‘is’ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
url-pattern
Advanced tools
url-pattern is easy pattern matching and segment extraction for urls, domains, filepaths and any string composed of segments joined by a separator character
The url-pattern npm package is a utility for matching URL patterns and extracting parameters from URLs. It is useful for routing in web applications, where you need to determine which code to execute based on the URL path.
Pattern Matching
This feature allows you to define URL patterns and match them against actual URLs. The example demonstrates how to create a pattern for user IDs and match it against a URL, extracting the user ID as a parameter.
const UrlPattern = require('url-pattern');
const pattern = new UrlPattern('/users/:id');
const match = pattern.match('/users/123');
console.log(match); // { id: '123' }
Reverse URL Generation
This feature allows you to generate URLs from a pattern and a set of parameters. The example shows how to create a URL for a specific user ID using the pattern.
const UrlPattern = require('url-pattern');
const pattern = new UrlPattern('/users/:id');
const url = pattern.stringify({ id: '123' });
console.log(url); // '/users/123'
Wildcard Matching
This feature allows you to use wildcards in your URL patterns to match any sequence of characters. The example demonstrates how to match any file path under the '/files' directory.
const UrlPattern = require('url-pattern');
const pattern = new UrlPattern('/files/*');
const match = pattern.match('/files/images/photo.jpg');
console.log(match); // { _: 'images/photo.jpg' }
The path-to-regexp package is another utility for matching URL patterns and extracting parameters. It is more flexible and powerful than url-pattern, supporting advanced pattern matching features like optional parameters and custom parameter patterns.
The route-parser package provides similar functionality for defining and matching URL patterns. It is simpler and more lightweight compared to url-pattern, making it a good choice for smaller projects or when you need a minimal solution.
The url-patterns package is another alternative for URL pattern matching and parameter extraction. It offers a similar API to url-pattern but includes additional features like query string parsing and more advanced pattern matching capabilities.
url-pattern is easy pattern matching and segment extraction for urls, domains, filepaths and any string composed of segments joined by a separator character
check out passage if you are looking for simple composable routing that builds on top of url-pattern
npm install url-pattern
require with commonjs:
var Pattern = require('url-pattern');
lib/url-pattern.js can be used in the browser. it supports AMD as well.
var pattern = new Pattern('/users/:id');
the default separator is /
. you can pass a custom separator
as the second argument.
match returns the extracted parameters or null
if there was no match:
pattern.match('/users/5'); // => {id: '5'}
pattern.match('/projects/5'); // => null
var regexPattern = new Pattern(/\/test\/(.*)/);
if the pattern was created from a regex an array of the captured groups is returned on match:
regexPattern.match('/test/users'); // => ['users']
regexPattern.match('/users/test'); // => null
var wildcardPattern = new Pattern('*/users/:id/*');
wildcard matches are collected in the _
property:
wildcardPattern.match('/api/v1/users/10/followers/20');
// => {id: '10', _: ['/api/v1', 'followers/20']}
var optionalPattern = new Pattern('(/)users(/:foo)/bar(/*)');
optional matches are stored in the corresponding property, if they exist.
optionalPattern.match('users/bar');
// => {}
optionalPattern.match('/users/bar');
// => {}
optionalPattern.match('/users/biff/bar');
// => {foo: 'biff'}
optionalPattern.match('/users/biff/bar/beep/boop');
// => {foo: 'biff', _: ['beep/boop']}
var pattern = new Pattern(':sub.google.com', '.');
the default separator is /
. you can pass a custom separator
as the second argument to Pattern
.
match returns the extracted parameters or null
if there was no match:
pattern.match('www.google.com'); // => {sub: 'www'}
pattern.match('www.google.io'); // => null
var regexPattern = new Pattern(/example\.(.*)/);
if the pattern was created from a regex an array of the captured groups is returned on match:
regexPattern.match('example.com'); // => ['com']
regexPattern.match('google.com'); // => null
var wildcardPattern = new Pattern('*.:sub.google.*');
wildcard matches are collected in the _
property:
wildcardPattern.match('subsub.www.google.com');
// => {sub: 'www', _: ['subsub', 'com']}
instead of
var urlPattern = require('url-pattern');
var pattern = urlPattern.newPattern('/example');
now use
var Pattern = require('url-pattern');
var pattern = new Pattern('/example');
TLDR: bugfixes, issues and discussion are always welcome. ask me before implementing new features.
i will happily merge pull requests that fix bugs with reasonable code.
i will only merge pull requests that modify/add functionality if the changes align with my goals for this package and only if the changes are well written, documented and tested.
communicate: write an issue to start a discussion before writing code that may or may not get merged.
:
and `[^a-zA-Z0-9]#{
and }
FAQs
easier than regex string matching patterns for urls and other strings. turn strings into data or data into strings.
The npm package url-pattern receives a total of 247,721 weekly downloads. As such, url-pattern popularity was classified as popular.
We found that url-pattern 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.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.