Function escapeRegExp

Escapes a string to be used in a regular expression. From: https://stackoverflow.com/a/3561711

const badName = "([{^|";
const regex = new RegExp(`^(maciek|${escapeRegExp(badName)})$`); // won't crash
regex.test(badName); // true
  • Parameters

    • string: string

      string to escape

    Returns string