1 var toString = require('./toString');
4 * Used to match `RegExp`
5 * [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns).
7 var reRegExpChar = /[\\^$.*+?()[\]{}|]/g,
8 reHasRegExpChar = RegExp(reRegExpChar.source);
11 * Escapes the `RegExp` special characters "^", "$", "\", ".", "*", "+",
12 * "?", "(", ")", "[", "]", "{", "}", and "|" in `string`.
18 * @param {string} [string=''] The string to escape.
19 * @returns {string} Returns the escaped string.
22 * _.escapeRegExp('[lodash](https://lodash.com/)');
23 * // => '\[lodash\]\(https://lodash\.com/\)'
25 function escapeRegExp(string) {
26 string = toString(string);
27 return (string && reHasRegExpChar.test(string))
28 ? string.replace(reRegExpChar, '\\$&')
32 module.exports = escapeRegExp;