Skip to content

@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 ​

ParameterTypeDescription
flagsstringThe 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 ​

ParameterType
templateTemplateStringsArray
...argsRegExp[]

Returns ​

RegExp

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 ​

ParameterTypeDescription
templateTemplateStringsArrayThe template string array containing the static parts of the regular expression.
...argsRegExp[]The interpolated RegExp objects to be inserted between the template strings.

Returns ​

RegExp

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'