UNPKG

469 BJavaScriptView Raw
1export default function escapeStringRegexp(string) {
2 if (typeof string !== 'string') {
3 throw new TypeError('Expected a string');
4 }
5
6 // Escape characters with special meaning either inside or outside character sets.
7 // 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.
8 return string
9 .replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
10 .replace(/-/g, '\\x2d');
11}