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