{"library":"path-to-regexp","title":"Express-style Path to RegExp Utility","description":"path-to-regexp is a robust JavaScript/TypeScript utility for converting Express-style path strings, such as `/user/:name`, into regular expressions. It is widely used in routing libraries to match URLs against defined patterns, supporting features like named parameters (`:foo`), wildcards (`*splat`), and optional segments (`{/:id}`). The library also provides reverse functionality through its `compile` and `stringify` methods, allowing parameters to be transformed back into path strings. Currently at version `8.4.2`, it maintains an active development cadence with regular updates focusing on performance enhancements, bundle size reduction, and critical security fixes. Its primary differentiator is its comprehensive feature set for complex path matching and generation, making it a foundational component for many web frameworks and routers. It explicitly states its purpose for ordered data like paths, not arbitrary data like query strings. This package ships with TypeScript types.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install path-to-regexp"],"cli":null},"imports":["import { match } from 'path-to-regexp'","import { pathToRegexp } from 'path-to-regexp'","import { compile } from 'path-to-regexp'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { match, compile, pathToRegexp } from 'path-to-regexp';\n\n// 1. Basic Parameter Matching\nconst userMatcher = match<{ id: string }>('/user/:id');\nconst userMatchResult = userMatcher('/user/123');\nconsole.log('User Match:', userMatchResult); // e.g., { path: '/user/123', params: { id: '123' } }\n\n// 2. Wildcard Matching\nconst splatMatcher = match<{ splat: string[] }>('/*splat');\nconst splatMatchResult = splatMatcher('/foo/bar/baz');\nconsole.log('Splat Match:', splatMatchResult); // e.g., { path: '/foo/bar/baz', params: { splat: ['foo', 'bar', 'baz'] } }\n\n// 3. Optional Segments\nconst optionalMatcher = match<{ id?: string }>('/items{/:id}');\nconst optionalMatchResult1 = optionalMatcher('/items');\nconst optionalMatchResult2 = optionalMatcher('/items/456');\nconsole.log('Optional Match (no ID):', optionalMatchResult1); // e.g., { path: '/items', params: {} }\nconsole.log('Optional Match (with ID):', optionalMatchResult2); // e.g., { path: '/items/456', params: { id: '456' } }\n\n// 4. Compiling/Reverse Routing\nconst toUserPath = compile<{ id: string }>('/profile/:id');\nconst compiledPath = toUserPath({ id: 'john-doe' });\nconsole.log('Compiled Path:', compiledPath); // e.g., '/profile/john-doe'\n\n// 5. Raw RegExp generation\nconst { regexp, keys } = pathToRegexp('/data/:category/:item');\nconsole.log('RegExp:', regexp); // e.g., /^\\/data\\/(?:([^\\/]+?))\\/(?:([^\\/]+?))\\/?$/i\nconsole.log('Keys:', keys);     // e.g., [{ name: 'category', ... }, { name: 'item', ... }]\nconst execResult = regexp.exec('/data/electronics/laptop');\nif (execResult) {\n  const params: { [key: string]: string } = {};\n  keys.forEach((key, i) => {\n    params[key.name] = execResult[i + 1];\n  });\n  console.log('RegExp Exec Params:', params);\n} // e.g., RegExp Exec Params: { category: 'electronics', item: 'laptop' }","lang":"typescript","description":"Demonstrates path matching with parameters, wildcards, and optional segments, along with reverse path compilation and direct RegExp generation.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}