Registry / web-framework / regexparam

regexparam

JSON →
library3.0.0jsnpmunverified

regexparam is a minimalist (399B) utility designed to convert URL-like route patterns (e.g., `/users/:id`) into JavaScript `RegExp` objects, extracting parameter keys in the process. It serves as a more lightweight and focused alternative to libraries like `path-to-regexp`. The current stable version is `3.0.0`. Releases occur periodically, addressing bug fixes and introducing minor breaking changes, typically every few months for major versions. Key differentiators include its extremely small footprint and its specific focus on basic routing patterns: static, parameter (with or without suffixes), optional parameters, wildcards, and optional wildcards. Unlike more comprehensive routing solutions, regexparam only handles parsing string patterns and does not manage a `keys` dictionary directly or mutate variables. It ships with CommonJS, ESModule, and UMD formats and includes TypeScript type definitions.

npm install regexparam
INSTALL
IMPORT
SIG · REGEXPARAM
R
regexparam
web-frameworkjavascriptv3.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
import { parse } from 'regexparam'
import regexparam from 'regexparam'
Since v2.0.0, `parse` is a named export. Previously, it was the default export.
inject
import { inject } from 'regexparam'
const inject = require('regexparam').inject
`inject` is a named export, primarily used with ESM. For CommonJS, you might need `const { inject } = require('regexparam')` in some environments, but ESM is preferred.
Result type
import type { Result } from 'regexparam'
TypeScript type for the return value of `parse` (object with `keys` and `pattern`).

Demonstrates how to parse a route pattern into a RegExp and keys, test paths against it, extract parameters, and inject parameters into a route string using `parse` and `inject`.

import { parse, inject } from 'regexparam'; // A helper function to extract parameters from a matched path function exec(path: string, result: ReturnType<typeof parse>): Record<string, string | null> { let i = 0; const out: Record<string, string | null> = {}; const matches = result.pattern.exec(path); if (!matches) { return {}; // No match } while (i < result.keys.length) { // Matches array index starts from 1 for captured groups out[result.keys[i]] = matches[++i] || null; } return out; } // Example 1: Parsing a route with optional parameters const route1 = parse('/books/:genre/:title?'); console.log('Route 1 pattern:', route1.pattern.source); console.log('Route 1 keys:', route1.keys); const path1a = '/books/horror'; console.log(`Matching "${path1a}":`, route1.pattern.test(path1a)); console.log(`Extracted params for "${path1a}":`, exec(path1a, route1)); const path1b = '/books/science-fiction/dune'; console.log(`Matching "${path1b}":`, route1.pattern.test(path1b)); console.log(`Extracted params for "${path1b}":`, exec(path1b, route1)); // Example 2: Injecting parameters into a route const injectedPath1 = inject('/users/:id', { id: 'alice' }); console.log('Injected path (user alice):', injectedPath1); const injectedPath2 = inject('/posts/:slug/*', { slug: 'my-post', '*': 'comments/123' }); console.log('Injected path (post with wildcard):', injectedPath2); // Example 3: Handling missing non-optional parameters during injection const incompleteInjection = inject('/products/:category/:item', { category: 'electronics' }); console.log('Incomplete injection:', incompleteInjection); // Missing 'item' parameter
Debug
Known issues
breakingThe behavior of optional wildcard patterns (e.g., `/books/*?`) has been corrected in v3.0.0. Previously, the optional part was ignored, making them behave like regular wildcards. This fix changes the generated `RegExp` and thus the matching behavior for these patterns.
fix
Review all routes utilizing optional wildcard patterns and verify their matching behavior against the new specification. Adjust application logic if the previous, incorrect behavior was relied upon.
affects: >=3.0.0
breakingThe default export of the library was converted to a named `parse` export in v2.0.0. This means the import method for the primary parsing function has changed.
fix
Update import statements from `import regexparam from 'regexparam'` to `import { parse } from 'regexparam'` for ESM. For CommonJS, change `const regexparam = require('regexparam'); regexparam(...)` to `const { parse } = require('regexparam'); parse(...)`.
affects: >=2.0.0
breakingVersion 2.0.0 increased the minimum required Node.js runtime from 6.x to 8.x.
fix
Ensure your Node.js environment is version 8 or higher before upgrading to regexparam v2.0.0 or later.
affects: >=2.0.0
gotchaWhen matching or testing against a generated `RegExp` pattern, the input path string MUST begin with a leading slash (`/`). Paths without a leading slash will not match correctly.
fix
Always prepend a leading slash to any path string passed to `pattern.test()` or `pattern.exec()` methods derived from `regexparam`.
affects: >=1.0.0
gotchaThe `inject` function will leave non-optional parameter placeholders in the output string if the corresponding value is not provided in the parameters object.
fix
Ensure all non-optional parameters defined in your route pattern are provided to the `inject` function to ensure complete path generation.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: regexparam is not a function
Attempting to call `require('regexparam')()` directly in CommonJS after v2.0.0, where the default export was removed.
fix
Change your CommonJS import and usage from `const regexparam = require('regexparam'); regexparam('/path')` to `const { parse } = require('regexparam'); parse('/path')`.
SyntaxError: The requested module 'regexparam' does not provide an export named 'parse'
Using `import { parse } from 'regexparam'` with a version prior to v2.0.0, where `parse` was not a named export but the default export.
fix
Either update `regexparam` to v2.0.0 or later, or change your import to `import regexparam from 'regexparam'` and use `regexparam('/path')`.
Route with optional wildcard /users/*? matches incorrectly.
Behavioral change in optional wildcard matching between v2.x and v3.x of `regexparam`.
fix
Upgrade to `regexparam` v3.0.0 and thoroughly test routes using optional wildcards (`*?`) to confirm expected matching behavior.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Amazon
1
Resources
regexparam — npm install regexparam · libregistry