### Install regexparam Source: https://www.npmjs.com/package/regexparam?activeTab=code Install the regexparam package using npm. ```bash $ npm install --save regexparam ``` -------------------------------- ### Inject URL Parameters Source: https://www.npmjs.com/package/regexparam?activeTab=code Demonstrates using the `inject` function to construct URLs by substituting parameters in a route pattern. ```javascript inject('/users/:id', { id: 'lukeed' }); //=> '/users/lukeed' inject('/movies/:title.mp4', { title: 'narnia' }); //=> '/movies/narnia.mp4' inject('/:foo/:bar?/:baz?', { foo: 'aaa' }); //=> '/aaa' inject('/:foo/:bar?/:baz?', { foo: 'aaa', baz: 'ccc' }); //=> '/aaa/ccc' inject('/posts/:slug/*', { slug: 'hello', }); //=> '/posts/hello' inject('/posts/:slug/*', { slug: 'hello', '*': 'x/y/z', }); //=> '/posts/hello/x/y/z' // Missing non-optional value // ~> keeps the pattern in output inject('/hello/:world', { abc: 123 }); //=> '/hello/:world' ``` -------------------------------- ### Parse Route with Parameter and Suffix Source: https://www.npmjs.com/package/regexparam?activeTab=code Demonstrates parsing a route with a parameter followed by a specific suffix (e.g., file extension). ```javascript let bar = parse('/movies/:title.(mp4|mov)'); // bar.pattern => /^\/movies\/([^\/]+?)\.(mp4|mov)\/Optional Wildcard?$/i // bar.keys => ['title'] bar.pattern.test('/movies/narnia'); //=> false bar.pattern.test('/movies/narnia.mp3'); //=> false bar.pattern.test('/movies/narnia.mp4'); //=> true exec('/movies/narnia.mp4', bar); //=> { title: 'narnia' } ``` -------------------------------- ### Parse Route with Parameter and Optional Parameter Source: https://www.npmjs.com/package/regexparam?activeTab=code Demonstrates parsing a route with a required parameter and an optional parameter. The `exec` function shows how to assign matched parameters to an object. ```javascript import { parse, inject } from 'regexparam'; // Example param-assignment function exec(path, result) { let i=0, out={}; let matches = result.pattern.exec(path); while (i < result.keys.length) { out[ result.keys[i] ] = matches[++i] || null; } return out; } // Parameter, with Optional Parameter // --- let foo = parse('/books/:genre/:title?') // foo.pattern => /^\/books\/([^\/]+?)(?:\/([^\/]+?))?\/?$/i // foo.keys => ['genre', 'title'] foo.pattern.test('/books/horror'); //=> true foo.pattern.test('/books/horror/goosebumps'); //=> true exec('/books/horror', foo); //=> { genre: 'horror', title: null } exec('/books/horror/goosebumps', foo); //=> { genre: 'horror', title: 'goosebumps' } ``` -------------------------------- ### Import regexparam with Deno Source: https://www.npmjs.com/package/regexparam?activeTab=dependencies Import the regexparam library for use with Deno. Supports imports from the official Deno registry or third-party CDNs. ```javascript import regexparam from 'https://deno.land/x/regexparam/src/index.js'; ``` ```javascript import regexparam from 'https://cdn.skypack.dev/regexparam'; ``` ```javascript import regexparam from 'https://esm.sh/regexparam'; ``` -------------------------------- ### Import Regexparam in Deno Source: https://www.npmjs.com/package/regexparam?activeTab=code Import the regexparam library in Deno using the official registry or third-party CDNs. Versioned URLs are also supported. ```javascript // The official Deno registry: import regexparam from 'https://deno.land/x/regexparam/src/index.js'; // Third-party CDNs with ESM support: import regexparam from 'https://cdn.skypack.dev/regexparam'; import regexparam from 'https://esm.sh/regexparam'; ``` -------------------------------- ### Parse Route with Wildcard Source: https://www.npmjs.com/package/regexparam?activeTab=code Demonstrates parsing a route that includes a wildcard character ('*') to match any path segment. ```javascript let baz = parse('users/*'); // baz.pattern => /^\/users\/(.*)\/?$/i // baz.keys => ['*'] baz.pattern.test('/users'); //=> false baz.pattern.test('/users/lukeed'); //=> true baz.pattern.test('/users/'); //=> true ``` -------------------------------- ### Parse Route with Optional Wildcard Source: https://www.npmjs.com/package/regexparam?activeTab=code Demonstrates parsing a route that includes an optional wildcard character ('*?'). ```javascript let baz = parse('/users/*?'); // baz.pattern => /^\/users(?:\/(.*))?(?=$|\/)/i // baz.keys => ['*'] baz.pattern.test('/users'); //=> true baz.pattern.test('/users/lukeed'); //=> true baz.pattern.test('/users/'); //=> true ``` -------------------------------- ### Parse route with parameter and suffix Source: https://www.npmjs.com/package/regexparam?activeTab=readme Convert a route string with a parameter and a suffix (e.g., file extension) into a RegExp. The `exec` helper function shows how to extract the parameter value. ```javascript let bar = parse('/movies/:title.(mp4|mov)'); // bar.pattern => /^\/movies\/([^\/]+?)\.(mp4|mov)\/ R?$/i // bar.keys => ['title'] bar.pattern.test('/movies/narnia'); //=> false bar.pattern.test('/movies/narnia.mp3'); //=> false bar.pattern.test('/movies/narnia.mp4'); //=> true exec('/movies/narnia.mp4', bar); //=> { title: 'narnia' } ``` -------------------------------- ### regexparam.inject(pattern: string, data: object) Source: https://www.npmjs.com/package/regexparam?activeTab=dependencies Injects values into a route pattern string to create a full path. ```APIDOC ## inject(pattern: string, data: object) ### Description Injects values into a route pattern string to create a full path. ### Parameters #### Path Parameters - **pattern** (string) - Required - The route pattern string. - **data** (object) - Required - An object containing key-value pairs to inject into the pattern. ### Request Example ```javascript import { inject } from 'regexparam'; let path = inject('/users/:id', { id: 'lukeed' }); // path => '/users/lukeed' let path = inject('/posts/:slug/*', { slug: 'hello', '*': 'x/y/z' }); // path => '/posts/hello/x/y/z' ``` ### Response #### Success Response (200) - **string** - The constructed path string. ### Response Example ```json "/users/lukeed" ``` ``` -------------------------------- ### regexparam.parse(input: RegExp) Source: https://www.npmjs.com/package/regexparam?activeTab=dependents Accepts a RegExp directly and returns it unchanged, with `keys` set to `false`. This allows for fine-tuned control over the pattern matching. ```APIDOC ## regexparam.parse(input: RegExp) ### Description When a RegExp is provided as input, `regexparam` returns the RegExp directly without modification. In this case, the `keys` property of the returned object will be `false`, and the `pattern` property will be identical to the input RegExp. ### Parameters #### Path Parameters - **input** (RegExp) - Required - The regular expression to use directly. ### Returns An object with the following properties: - **keys** (false) - Indicates that no keys were parsed from the input. - **pattern** (RegExp) - The input regular expression. ### Examples ```javascript import regexparam from 'regexparam'; // Named capture group const named = regexparam.parse(/^\/posts[/](?[0-9]{4})[/](?[0-9]{2})[/](?[^\/]+)/i); const { groups } = named.pattern.exec('/posts/2019/05/hello-world'); console.log(groups); //=> { year: '2019', month: '05', title: 'hello-world' } // Widely supported / "Old-fashioned" const legacy = regexparam.parse(/^\/posts[/]([0-9]{4})[/]([0-9]{2})[/]([^\/]+)/i); const [url, year, month, title] = legacy.pattern.exec('/posts/2019/05/hello-world'); console.log(year, month, title); //=> 2019 05 hello-world ``` ``` -------------------------------- ### regexparam.inject(pattern: string, params: object) Source: https://www.npmjs.com/package/regexparam?activeTab=dependents Injects values into a route pattern string, replacing placeholders with provided parameter values. ```APIDOC ## regexparam.inject(pattern: string, params: object) ### Description Replaces placeholders in a route pattern string with corresponding values from the `params` object. If a non-optional parameter is missing, the placeholder remains in the output string. ### Parameters #### Path Parameters - **pattern** (string) - Required - The route pattern string containing placeholders (e.g., `/:id`). - **params** (object) - Required - An object where keys are parameter names and values are the strings to inject. ### Returns A string with the placeholders in the `pattern` replaced by values from `params`. ### Examples ```javascript import { inject } from 'regexparam'; inject('/users/:id', { id: 'lukeed' }); //=> '/users/lukeed' inject('/movies/:title.mp4', { title: 'narnia' }); //=> '/movies/narnia.mp4' inject('/:foo/:bar?/:baz?', { foo: 'aaa' }); //=> '/aaa' inject('/:foo/:bar?/:baz?', { foo: 'aaa', baz: 'ccc' }); //=> '/aaa/ccc' inject('/posts/:slug/*', { slug: 'hello' }); //=> '/posts/hello' inject('/posts/:slug/*', { slug: 'hello', '*': 'x/y/z' }); //=> '/posts/hello/x/y/z' // Missing non-optional value // ~> keeps the pattern in output inject('/hello/:world', { abc: 123 }); //=> '/hello/:world' ``` ``` -------------------------------- ### Inject values into route pattern Source: https://www.npmjs.com/package/regexparam?activeTab=dependencies Uses the 'inject' function to construct a URL string by replacing placeholders in a route pattern with provided values. Handles optional parameters and wildcards. ```javascript // Injecting // --- inject('/users/:id', { id: 'lukeed' }); //=> '/users/lukeed' inject('/movies/:title.mp4', { title: 'narnia' }); //=> '/movies/narnia.mp4' inject('/:foo/:bar?/:baz?', { foo: 'aaa' }); //=> '/aaa' inject('/:foo/:bar?/:baz?', { foo: 'aaa', baz: 'ccc' }); //=> '/aaa/ccc' inject('/posts/:slug/*', { slug: 'hello', }); //=> '/posts/hello' inject('/posts/:slug/*', { slug: 'hello', '*': 'x/y/z', }); //=> '/posts/hello/x/y/z' // Missing non-optional value // ~> keeps the pattern in output inject('/hello/:world', { abc: 123 }); //=> '/hello/:world' ``` -------------------------------- ### Parse RegExp with Positional Capture Groups Source: https://www.npmjs.com/package/regexparam?activeTab=code Demonstrates using regexparam with a pre-defined RegExp using standard capture groups. The matched segments are returned as an array. ```javascript // Widely supported / "Old-fashioned" const named = regexparam.parse(/^\/posts[/]([0-9]{4})[/]([0-9]{2})[/]([^\/]+)/i); const [url, year, month, title] = named.pattern.exec('/posts/2019/05/hello-world'); console.log(year, month, title); //=> 2019 05 hello-world ``` -------------------------------- ### regexparam.parse(input: string) Source: https://www.npmjs.com/package/regexparam?activeTab=dependents Converts a route pattern string into an object containing a RegExp pattern and an array of keys. Supports static paths, parameters, parameters with suffixes, optional parameters, and wildcards. ```APIDOC ## regexparam.parse(input: string) ### Description Converts a route pattern string into an object with `keys` and `pattern` properties. The `pattern` is a RegExp object, and `keys` is an array of parameter names. ### Parameters #### Path Parameters - **input** (string) - Required - The route pattern string to parse. ### Returns An object with the following properties: - **keys** (Array<string>) - An array of parameter names in the order they appeared. - **pattern** (RegExp) - The generated regular expression for matching paths. ### Examples ```javascript import { parse } from 'regexparam'; // Parameter, with Optional Parameter let foo = parse('/books/:genre/:title?'); // foo.pattern => /^\/books\/([^\/]+?)(?:\/([^\/]+?))?\/?$/i // foo.keys => ['genre', 'title'] // Parameter, with suffix let bar = parse('/movies/:title.(mp4|mov)'); // bar.pattern => /^\/movies\/([^\/]+?)\.(mp4|mov)\/$/i // bar.keys => ['title'] // Wildcard let baz = parse('users/*'); // baz.pattern => /^\/users\/(.*)\/?$/i // baz.keys => ['*'] // Optional Wildcard let qux = parse('/users/*?'); // qux.pattern => /^\/users(?:\/(.*))?(?=$|\/)/i // qux.keys => ['*'] ``` ``` -------------------------------- ### Parse RegExp with Named Capture Groups Source: https://www.npmjs.com/package/regexparam?activeTab=code Shows how to use regexparam with a pre-defined RegExp that includes named capture groups. The `groups` object contains the extracted named parameters. ```javascript // Named capture group const named = regexparam.parse(/^\/posts[/](?<year>[0-9]{4})[/](?<month>[0-9]{2})[/](?<title>[^\/]+)/i); const { groups } = named.pattern.exec('/posts/2019/05/hello-world'); console.log(groups); //=> { year: '2019', month: '05', title: 'hello-world' } ``` -------------------------------- ### regexparam.parse(input: string, loose?: boolean) Source: https://www.npmjs.com/package/regexparam?activeTab=code Parses a route pattern string into an equivalent RegExp pattern and collects parameter names. If the input is already a RegExp, it is used as is. The function returns an object containing the `keys` (parameter names or false) and the `pattern` (a RegExp instance). ```APIDOC ## regexparam.parse(input: string, loose?: boolean) ### Description Parses a route pattern string into an equivalent RegExp pattern and collects parameter names. If the input is already a RegExp, it is used as is. The function returns an object containing the `keys` (parameter names or false) and the `pattern` (a RegExp instance). ### Parameters #### input Type: `string` or `RegExp` When `input` is a string, it's treated as a route pattern and an equivalent RegExp is generated. It does not matter if `input` strings begin with a `/` — it will be added if missing. When `input` is a RegExp, it will be used **as is** – no modifications will be made. #### loose Type: `boolean` Default: `false` Should the `RegExp` match URLs that are longer than the `str` pattern itself? By default, the generated `RegExp` will test that the URL begins and _ends with_ the pattern. When `input` is a RegExp, the `loose` argument is ignored! ### Returns Type: `Object` Returns a `{ keys, pattern }` object, where `pattern` is always a `RegExp` instance and `keys` is either `false` or a list of extracted parameter names. ### Examples ```javascript const { parse } = require('regexparam'); parse('/users').pattern.test('/users/lukeed'); //=> false parse('/users', true).pattern.test('/users/lukeed'); //=> true parse('/users/:name').pattern.test('/users/lukeed/repos'); //=> false parse('/users/:name', true).pattern.test('/users/lukeed/repos'); //=> true ``` ``` -------------------------------- ### regexparam.parse(input: string | RegExp) Source: https://www.npmjs.com/package/regexparam?activeTab=dependencies Parses a route pattern string or a RegExp into an object containing a RegExp pattern and an array of keys. If a RegExp is provided, it returns the RegExp itself and sets keys to false. ```APIDOC ## regexparam.parse(input: string | RegExp) ### Description Parses a route pattern string or a RegExp into an object containing a RegExp pattern and an array of keys. If a RegExp is provided, it returns the RegExp itself and sets keys to false. ### Parameters #### Path Parameters - **input** (string | RegExp) - Required - The route pattern string or a RegExp to parse. ### Response #### Success Response (200) - **keys** (Array<string> | false) - An array of parameter names in the order they appeared, or false if a RegExp was provided. - **pattern** (RegExp) - The generated regular expression. ### Request Example ```javascript import { parse } from 'regexparam'; // String input let result = parse('/users/:id'); // result.pattern => /^\/users\/([^\/]+?)\/$/i // result.keys => ['id'] // RegExp input let result = parse(/^\/users\/([^\/]+?)\/$/i); // result.pattern => /^\/users\/([^\/]+?)\/$/i // result.keys => false ``` ### Response Example ```json { "keys": ["id"], "pattern": /^\/users\/([^\/]+?)\/$/i } ``` ``` -------------------------------- ### regexparam.parse Source: https://www.npmjs.com/package/regexparam?activeTab=readme Parses a route pattern string or RegExp into an equivalent RegExp pattern and collects parameter names. Handles string inputs by generating a RegExp, and leaves RegExp inputs unchanged. The `loose` option controls whether the generated RegExp matches URLs longer than the pattern. ```APIDOC ## regexparam.parse(input: string | RegExp, loose?: boolean) ### Description Parses a route pattern into an equivalent RegExp pattern. Also collects the names of pattern's parameters as a `keys` array. An `input` that's already a RegExp is kept as is, and `regexparam` makes no additional insights. Returns a `{ keys, pattern }` object, where `pattern` is always a `RegExp` instance and `keys` is either `false` or a list of extracted parameter names. ### Parameters #### input Type: `string` or `RegExp` When `input` is a string, it's treated as a route pattern and an equivalent RegExp is generated. It does not matter if `input` strings begin with a `/` — it will be added if missing. When `input` is a RegExp, it will be used **as is** – no modifications will be made. #### loose Type: `boolean` Default: `false` Should the `RegExp` match URLs that are longer than the `str` pattern itself? By default, the generated `RegExp` will test that the URL begins and _ends with_ the pattern. When `input` is a RegExp, the `loose` argument is ignored! ### Returns Type: `Object` Returns a `{ keys, pattern }` object. ### Example ```javascript const { parse } = require('regexparam'); parse('/users').pattern.test('/users/lukeed'); //=> false parse('/users', true).pattern.test('/users/lukeed'); //=> true parse('/users/:name').pattern.test('/users/lukeed/repos'); //=> false parse('/users/:name', true).pattern.test('/users/lukeed/repos'); //=> true ``` ``` -------------------------------- ### regexparam.inject(pattern: string, values: object) Source: https://www.npmjs.com/package/regexparam?activeTab=code Returns a new string by replacing the `pattern` segments/parameters with their matching values. Named segments that do not have a `values` match will be kept in the output, except for optional and wildcard segments. ```APIDOC ## regexparam.inject(pattern: string, values: object) ### Description Returns a new string by replacing the `pattern` segments/parameters with their matching values. Named segments that do not have a `values` match will be kept in the output, except for optional and wildcard segments. ### Parameters #### pattern Type: `string` The route pattern that to receive injections. #### values Type: `Record<string, string>` The values to be injected. The keys within `values` must match the `pattern`'s segments in order to be replaced. To replace a wildcard segment (eg, `/*`), define a `values['*']` key. ### Returns Type: `string` A new string with replaced segments. ``` -------------------------------- ### regexparam.inject(pattern: string, values: object) Source: https://www.npmjs.com/package/regexparam?activeTab=dependents Returns a new string by replacing the pattern segments/parameters with their matching values. Handles named segments and wildcard segments. ```APIDOC ## regexparam.inject(pattern: string, values: object) ### Description Returns a new string by replacing the `pattern` segments/parameters with their matching values. **Important:** Named segments (eg, `/:name`) that _do not_ have a `values` match will be kept in the output. This is true _except for_ optional segments (eg, `/:name?`) and wildcard segments (eg, `/*`). ### Parameters #### pattern Type: `string` The route pattern that to receive injections. #### values Type: `Record<string, string>` The values to be injected. The keys within `values` must match the `pattern`'s segments in order to be replaced. **Note:** To replace a wildcard segment (eg, `/*`), define a `values['*']` key. ### Request Example ```javascript const { inject } = require('regexparam'); inject('/users/:name', { name: 'lukeed' }); //=> '/users/lukeed' inject('/users/:name?', { name: 'lukeed' }); //=> '/users/lukeed' inject('/users/:name?', {}); //=> '/users/' inject('/users/*', { '*': 'lukeed' }); //=> '/users/lukeed' ``` ### Response Returns a `string` with injected values. #### Success Response (200) - **string** - The new string with replaced segments. ``` -------------------------------- ### Parse Route with Suffix Source: https://www.npmjs.com/package/regexparam Parses a route string that includes a suffix, such as a file extension. The `parse` function generates a RegExp that accounts for the specified suffix. ```javascript let bar = parse('/movies/:title.(mp4|mov)'); // bar.pattern => /^\/movies\/([^\/]+?)\.(mp4|mov)\/$/i // bar.keys => ['title'] bar.pattern.test('/movies/narnia'); //=> false bar.pattern.test('/movies/narnia.mp3'); //=> false bar.pattern.test('/movies/narnia.mp4'); //=> true exec('/movies/narnia.mp4', bar); //=> { title: 'narnia' } ``` -------------------------------- ### Parse Route Patterns with Regexparam Source: https://www.npmjs.com/package/regexparam?activeTab=code Use `regexparam.parse` to convert string route patterns into RegExp objects. The `loose` option allows matching URLs longer than the pattern. The `keys` array is returned when the input is a string. ```javascript const { parse } = require('regexparam'); parse('/users').pattern.test('/users/lukeed'); //=> false parse('/users', true).pattern.test('/users/lukeed'); //=> true parse('/users/:name').pattern.test('/users/lukeed/repos'); //=> false parse('/users/:name', true).pattern.test('/users/lukeed/repos'); //=> true ``` -------------------------------- ### regexparam.inject Source: https://www.npmjs.com/package/regexparam Returns a new string by replacing the pattern segments/parameters with their matching values. Handles named segments and wildcard segments. ```APIDOC ## regexparam.inject(pattern: string, values: Record<string, string>) ### Description Returns a new string by replacing the `pattern` segments/parameters with their matching values. ### Parameters #### pattern Type: `string` The route pattern that to receive injections. #### values Type: `Record<string, string>` The values to be injected. The keys within `values` must match the `pattern`'s segments in order to be replaced. To replace a wildcard segment (eg, `/*`), define a `values['*']` key. ### Returns `string` - The new string with injected values. ### Important Notes Named segments (eg, `/:name`) that _do not_ have a `values` match will be kept in the output. This is true _except for_ optional segments (eg, `/:name?`) and wildcard segments (eg, `/*`). ``` -------------------------------- ### Inject Values into Route Pattern Source: https://www.npmjs.com/package/regexparam?activeTab=dependents Use `inject` to replace segments in a route pattern with corresponding values from an object. Named segments without a match in `values` are retained, except for optional or wildcard segments. ```javascript const { inject } = require('regexparam'); inject('/users/:name', { name: 'lukeed' }); //=> '/users/lukeed' inject('/users/:name?', { name: 'lukeed' }); //=> '/users/lukeed' inject('/users/*', { '*': 'lukeed' }); //=> '/users/lukeed' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.