index.js 469 B

1234567891011
  1. export default function escapeStringRegexp(string) {
  2. if (typeof string !== 'string') {
  3. throw new TypeError('Expected a string');
  4. }
  5. // Escape characters with special meaning either inside or outside character sets.
  6. // Use a simple backslash escape when it’s always valid, and a `\xnn` escape when the simpler form would be disallowed by Unicode patterns’ stricter grammar.
  7. return string
  8. .replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
  9. .replace(/-/g, '\\x2d');
  10. }