{"id":15802,"library":"regexparam","title":"Tiny Route Pattern to RegExp Utility","description":"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.","status":"active","version":"3.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/lukeed/regexparam","tags":["javascript","regexp","route","routing","inject","parse","typescript"],"install":[{"cmd":"npm install regexparam","lang":"bash","label":"npm"},{"cmd":"yarn add regexparam","lang":"bash","label":"yarn"},{"cmd":"pnpm add regexparam","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since v2.0.0, `parse` is a named export. Previously, it was the default export.","wrong":"import regexparam from 'regexparam'","symbol":"parse","correct":"import { parse } from 'regexparam'"},{"note":"`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.","wrong":"const inject = require('regexparam').inject","symbol":"inject","correct":"import { inject } from 'regexparam'"},{"note":"TypeScript type for the return value of `parse` (object with `keys` and `pattern`).","symbol":"Result type","correct":"import type { Result } from 'regexparam'"}],"quickstart":{"code":"import { parse, inject } from 'regexparam';\n\n// A helper function to extract parameters from a matched path\nfunction exec(path: string, result: ReturnType<typeof parse>): Record<string, string | null> {\n  let i = 0;\n  const out: Record<string, string | null> = {};\n  const matches = result.pattern.exec(path);\n\n  if (!matches) {\n    return {}; // No match\n  }\n\n  while (i < result.keys.length) {\n    // Matches array index starts from 1 for captured groups\n    out[result.keys[i]] = matches[++i] || null;\n  }\n  return out;\n}\n\n// Example 1: Parsing a route with optional parameters\nconst route1 = parse('/books/:genre/:title?');\nconsole.log('Route 1 pattern:', route1.pattern.source);\nconsole.log('Route 1 keys:', route1.keys);\n\nconst path1a = '/books/horror';\nconsole.log(`Matching \"${path1a}\":`, route1.pattern.test(path1a));\nconsole.log(`Extracted params for \"${path1a}\":`, exec(path1a, route1));\n\nconst path1b = '/books/science-fiction/dune';\nconsole.log(`Matching \"${path1b}\":`, route1.pattern.test(path1b));\nconsole.log(`Extracted params for \"${path1b}\":`, exec(path1b, route1));\n\n// Example 2: Injecting parameters into a route\nconst injectedPath1 = inject('/users/:id', { id: 'alice' });\nconsole.log('Injected path (user alice):', injectedPath1);\n\nconst injectedPath2 = inject('/posts/:slug/*', { slug: 'my-post', '*': 'comments/123' });\nconsole.log('Injected path (post with wildcard):', injectedPath2);\n\n// Example 3: Handling missing non-optional parameters during injection\nconst incompleteInjection = inject('/products/:category/:item', { category: 'electronics' });\nconsole.log('Incomplete injection:', incompleteInjection); // Missing 'item' parameter\n","lang":"typescript","description":"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`."},"warnings":[{"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.","message":"The 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.","severity":"breaking","affected_versions":">=3.0.0"},{"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(...)`.","message":"The 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.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Ensure your Node.js environment is version 8 or higher before upgrading to regexparam v2.0.0 or later.","message":"Version 2.0.0 increased the minimum required Node.js runtime from 6.x to 8.x.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Always prepend a leading slash to any path string passed to `pattern.test()` or `pattern.exec()` methods derived from `regexparam`.","message":"When 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure all non-optional parameters defined in your route pattern are provided to the `inject` function to ensure complete path generation.","message":"The `inject` function will leave non-optional parameter placeholders in the output string if the corresponding value is not provided in the parameters object.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Change your CommonJS import and usage from `const regexparam = require('regexparam'); regexparam('/path')` to `const { parse } = require('regexparam'); parse('/path')`.","cause":"Attempting to call `require('regexparam')()` directly in CommonJS after v2.0.0, where the default export was removed.","error":"TypeError: regexparam is not a function"},{"fix":"Either update `regexparam` to v2.0.0 or later, or change your import to `import regexparam from 'regexparam'` and use `regexparam('/path')`.","cause":"Using `import { parse } from 'regexparam'` with a version prior to v2.0.0, where `parse` was not a named export but the default export.","error":"SyntaxError: The requested module 'regexparam' does not provide an export named 'parse'"},{"fix":"Upgrade to `regexparam` v3.0.0 and thoroughly test routes using optional wildcards (`*?`) to confirm expected matching behavior.","cause":"Behavioral change in optional wildcard matching between v2.x and v3.x of `regexparam`.","error":"Route with optional wildcard /users/*? matches incorrectly."}],"ecosystem":"npm"}