Skip to content

Instantly share code, notes, and snippets.

View dacarley's full-sized avatar

David A. Carley dacarley

View GitHub Profile
@dacarley
dacarley / regex-tagged-template-string.ts
Created August 7, 2024 17:25
A tagged template string implementation for composing regexes
import { isRegExp, zipWith } from 'lodash';
export function regex(strings: TemplateStringsArray, ...values: any[]): RegExp {
const pattern = zipWith(strings, values, (str, value) => {
if (isRegExp(value)) {
value = value.source.replace(/^\^|\$$/g, '');
}
return str + (value ?? '');
}).join('');