Skip to content

Instantly share code, notes, and snippets.

@KrofDrakula
Created July 30, 2019 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KrofDrakula/f0ee95147ed0e8f5cb2bd5456e63ae3b to your computer and use it in GitHub Desktop.
Save KrofDrakula/f0ee95147ed0e8f5cb2bd5456e63ae3b to your computer and use it in GitHub Desktop.
const SPECIAL_CHARS = /[-\/\\^$*+?.()|[\]{}]/g;
const ESCAPED_CHAR = "\\$&";
const escapeRegExp = (s: string) => s.replace(SPECIAL_CHARS, ESCAPED_CHAR);
const regexp = (flags?: string) =>
(strings: string[], ...values: string[]) =>
new RegExp(strings[0] + values.map((value, index) => escapeRegExp(value) + strings[index + 1]).join(""), flags);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment