52878c1d84738e4a1985e2437e2baf81a8f4b028
[yaffs-website] / node_modules / grunt-legacy-util / node_modules / lodash / escapeRegExp.js
1 var toString = require('./toString');
2
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);
6
7 /**
8  * Escapes the `RegExp` special characters "^", "$", "\", ".", "*", "+",
9  * "?", "(", ")", "[", "]", "{", "}", and "|" in `string`.
10  *
11  * @static
12  * @memberOf _
13  * @category String
14  * @param {string} [string=''] The string to escape.
15  * @returns {string} Returns the escaped string.
16  * @example
17  *
18  * _.escapeRegExp('[lodash](https://lodash.com/)');
19  * // => '\[lodash\]\(https://lodash\.com/\)'
20  */
21 function escapeRegExp(string) {
22   string = toString(string);
23   return (string && reHasRegExpChar.test(string))
24     ? string.replace(reRegExpChar, '\\$&')
25     : string;
26 }
27
28 module.exports = escapeRegExp;