SIGN IN SIGN UP

A modern JavaScript utility library delivering modularity, performance, & extras.

0 0 9 JavaScript
2015-12-16 17:53:20 -08:00
define(['./toString'], function(toString) {
2016-04-01 23:28:48 -07:00
/**
* Used to match `RegExp`
2016-08-07 21:21:03 -07:00
* [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns).
2016-04-01 23:28:48 -07:00
*/
2015-12-16 17:53:20 -08:00
var reRegExpChar = /[\\^$.*+?()[\]{}|]/g,
reHasRegExpChar = RegExp(reRegExpChar.source);
/**
* Escapes the `RegExp` special characters "^", "$", "\", ".", "*", "+",
* "?", "(", ")", "[", "]", "{", "}", and "|" in `string`.
*
* @static
* @memberOf _
2016-03-26 00:00:01 -07:00
* @since 3.0.0
2015-12-16 17:53:20 -08:00
* @category String
* @param {string} [string=''] The string to escape.
* @returns {string} Returns the escaped string.
* @example
*
* _.escapeRegExp('[lodash](https://lodash.com/)');
* // => '\[lodash\]\(https://lodash\.com/\)'
*/
function escapeRegExp(string) {
string = toString(string);
return (string && reHasRegExpChar.test(string))
? string.replace(reRegExpChar, '\\$&')
: string;
}
return escapeRegExp;
});