mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
34 lines
1004 B
JavaScript
34 lines
1004 B
JavaScript
define(['../internal/baseToString'], function(baseToString) {
|
|
|
|
/**
|
|
* Used to match `RegExp` special characters.
|
|
* See this [article on `RegExp` characters](http://www.regular-expressions.info/characters.html#special)
|
|
* for more details.
|
|
*/
|
|
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;
|
|
});
|