Package Exports
- als-path-to-regexp
- als-path-to-regexp/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (als-path-to-regexp) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
als-path-to-regexp
als-path-to-regexp is a utility that allows developers to convert URL paths into regular expressions for flexible and comprehensive URL matching. This utility handles path segments with variables ({variable}), wildcards (*), fixed strings, and paths that contain combinations of special characters like ? and *.
als-path-to-regexp is a powerful utility designed to handle various path patterns and combinations for flexible URL matching. With comprehensive test coverage, it ensures accuracy and reliability in your web application routing.
Installation
To install als-path-to-regexp, use npm:
npm install als-path-to-regexpUsage
const pathToRegexp = require('als-path-to-regexp');
// Match a simple static path
const staticRoute = pathToRegexp('/test/path');
console.assert(staticRoute('/test/path') !== null, 'Static path should match');
// Match a path with a variable
const userRoute = pathToRegexp('/user/{id}');
const userMatch = userRoute('/user/123');
console.assert(userMatch !== null && userMatch.id === '123', 'Parameterized path should match and extract params correctly');
// Match a path with a wildcard (*)
const wildcardRoute = pathToRegexp('/search/*/test');
console.assert(wildcardRoute('/search/query/test') !== null, 'Wildcard path should match');
// Match a path with question marks (?)
const questionRoute = pathToRegexp('/docs/item_?1');
const questionMatch = questionRoute('/docs/item_a1');
console.assert(questionMatch !== null, 'Path with "?" should match any single character');Path Syntax
Variables (
{variable}): Extract parameters from a path by enclosing them in curly braces, e.g.,/user/{id}.Wildcard (
*): Match any segment of a path. For example,/images/*/thumbnailmatches any path segment between/images/and/thumbnail.Question Mark (
?): Represents any single character. For instance,/docs/item_?1matches/docs/item_a1or/docs/item_b1.Combination of Characters: Mix variables, wildcards, and question marks to create flexible patterns.
Advanced Examples
// Match a more complex path with multiple parameters and a wildcard
const complexRoute = pathToRegexp('/user/{id}/profile/{section}/*');
const complexMatch = complexRoute('/user/123/profile/settings/images');
console.assert(complexMatch !== null && complexMatch.id === '123' && complexMatch.section === 'settings', 'Complex path with parameters and wildcard should match and extract params correctly');
// Match a path with multiple wildcards
const multiWildcardRoute = pathToRegexp('/files/*/*');
console.assert(multiWildcardRoute('/files/images/avatar.jpg') !== null, 'Path with multiple wildcards should match');
// Handle extra slashes gracefully
const extraSlashRoute = pathToRegexp('/user///profile/{section}');
const extraSlashMatch = extraSlashRoute('/user/profile/settings');
console.assert(extraSlashMatch !== null && extraSlashMatch.section === 'settings', 'Path should ignore extra slashes and match correctly');
// Case-sensitive path matching
const caseSensitiveRoute = pathToRegexp('/User/{id}');
const caseSensitiveMatch = caseSensitiveRoute('/User/123');
console.assert(caseSensitiveMatch !== null && caseSensitiveMatch.id === '123', 'Path should match case-sensitive route');
console.assert(caseSensitiveRoute('/user/123') === null, 'Case-sensitive path should not match lowercase');Error Handling
If your path contains an invalid pattern, als-path-to-regexp will throw an error, providing details about what went wrong. For instance, using ? in static parts of the path will result in an error.
try {
pathToRegexp('/user/{id}/:');
} catch (e) {
console.error('Invalid path pattern:', e.message);
}Notes and Recommendations
- Normalization: Paths are normalized using the
als-normalize-urlpathpackage to ensure consistent behavior. - Special Characters: The utility supports special characters like
.,+,^,$,(),|,[,],{, and}by escaping them properly in regular expressions. - Multiple Wildcards: While you can use multiple wildcards, ensure that the pattern doesn't conflict with other path segments.