mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 07:17:50 +00:00
34 lines
1.1 KiB
JavaScript
34 lines
1.1 KiB
JavaScript
define(['../internal/baseToString'], function(baseToString) {
|
|
|
|
/**
|
|
* Used to match `RegExp` [special characters](http://www.regular-expressions.info/characters.html#special).
|
|
* In addition to special characters the forward slash is escaped to allow for
|
|
* easier `eval` use and `Function` compilation.
|
|
*/
|
|
var reRegExpChars = /[.*+?^${}()|[\]\/\\]/g,
|
|
reHasRegExpChars = RegExp(reRegExpChars.source);
|
|
|
|
/**
|
|
* Escapes the `RegExp` special characters "\", "/", "^", "$", ".", "|", "?",
|
|
* "*", "+", "(", ")", "[", "]", "{" and "}" in `string`.
|
|
*
|
|
* @static
|
|
* @memberOf _
|
|
* @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 = baseToString(string);
|
|
return (string && reHasRegExpChars.test(string))
|
|
? string.replace(reRegExpChars, '\\$&')
|
|
: string;
|
|
}
|
|
|
|
return escapeRegExp;
|
|
});
|