mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
33 lines
948 B
JavaScript
33 lines
948 B
JavaScript
var baseToString = require('../internal/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;
|
|
}
|
|
|
|
module.exports = escapeRegExp;
|