9a29df0ad21d7a058e07d960cdecbf6f878c7b06c4764e6b5ae42959a31c83d3369fec48ec3b858f494167b60ffc2a96cf3e6006d0923c7c731614c57bc0de 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. }