Skip to content

Format regex with string substitutions. Auto-escape RegExp special characters

License

Notifications You must be signed in to change notification settings

morpheus-87/regex-format

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

regex-format Build Status Dependency Status DevDependency Status

npm

Format regex with string substitutions.

Example

regexFormat(/^Author:\s+{#name} <{#email}>$/, {
  name: 'someone',
  email: '[email protected]'
})
//=> /^Author:\s+someone <someone@example\.com>$/

regexFormat(/^{}$/, 'foo$1')
//=> /^foo\$1$/

API

regexFormat(spec, [values]...)

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 {}).

Install

npm install regex-format

License

MIT

About

Format regex with string substitutions. Auto-escape RegExp special characters

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%