@technobuddha > library > RegExp > Construction
Function: re()
Creates a tagged template function for building regular expressions with optional specified flags.
Call Signature
ts
function re(flags: string): (template: TemplateStringsArray, ...args: RegExp[]) => RegExp;Defined in: re.ts:76
Creates a tagged template function for building regular expressions with the specified flags.
Parameters
| Parameter | Type | Description |
|---|---|---|
flags | string | The flags to apply to the resulting regular expression (e.g., 'g', 'i', 'm'). |
Returns
A tagged template function that constructs a RegExp object from a template string and interpolated RegExp patterns.
ts
(template: TemplateStringsArray, ...args: RegExp[]): RegExp;Parameters
| Parameter | Type |
|---|---|
template | TemplateStringsArray |
...args | RegExp[] |
Returns
Example
typescript
const bar = /bar/;
const regex = re('gi')`foo${bar}baz${bar}qux`;
// regex is a RegExp with pattern 'foo(?:bar)baz(?:bar)qux' and flags 'giu'Call Signature
ts
function re(template: TemplateStringsArray, ...args: RegExp[]): RegExp;Defined in: src/re.ts:90
Creates a new RegExp object from a template string and interpolated regular expressions.
Parameters
| Parameter | Type | Description |
|---|---|---|
template | TemplateStringsArray | The template string array containing the static parts of the regular expression. |
...args | RegExp[] | The interpolated RegExp objects to be inserted between the template strings. |
Returns
A new RegExp object constructed from the combined template and arguments.
Example
typescript
const bar = /bar/;
const regex = re`foo${bar}baz${bar}qux`;
// regex is a RegExp with pattern 'foo(?:bar)baz(?:bar)qux' and flags 'u'