1 var baseToString = require('../internal/baseToString'),
2 escapeRegExpChar = require('../internal/escapeRegExpChar');
5 * Used to match `RegExp` [syntax characters](http://ecma-international.org/ecma-262/6.0/#sec-patterns)
6 * and those outlined by [`EscapeRegExpPattern`](http://ecma-international.org/ecma-262/6.0/#sec-escaperegexppattern).
8 var reRegExpChars = /^[:!,]|[\\^$.*+?()[\]{}|\/]|(^[0-9a-fA-Fnrtuvx])|([\n\r\u2028\u2029])/g,
9 reHasRegExpChars = RegExp(reRegExpChars.source);
12 * Escapes the `RegExp` special characters "\", "/", "^", "$", ".", "|", "?",
13 * "*", "+", "(", ")", "[", "]", "{" 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 = baseToString(string);
27 return (string && reHasRegExpChars.test(string))
28 ? string.replace(reRegExpChars, escapeRegExpChar)
32 module.exports = escapeRegExp;