Format regex with string substitutions.
regexFormat(/^Author:\s+{#name} <{#email}>$/, {
name: 'someone',
email: '[email protected]'
})
//=> /^Author:\s+someone <someone@example\.com>$/
regexFormat(/^{}$/, 'foo$1')
//=> /^foo\$1$/
spec
is a RegExp, otherwise it is converted to RegExp. Flags are preserved.
The syntax for substitutions is the same as accepted by string-format, except that non-empty groups are prepended with a hash sign: {#0}
, {#foo.bar}
, etc (empty groups are still {}
).
npm install regex-format
MIT