mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 14:37:49 +00:00
* Enable escapeRegExp tests * Ensure escapeRegExp returns an empty string when passing falsy values * Add parens around expression in escapeRegExp
29 lines
819 B
JavaScript
29 lines
819 B
JavaScript
/**
|
|
* Used to match `RegExp`
|
|
* [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns).
|
|
*/
|
|
const reRegExpChar = /[\\^$.*+?()[\]{}|]/g
|
|
const reHasRegExpChar = RegExp(reRegExpChar.source)
|
|
|
|
/**
|
|
* Escapes the `RegExp` special characters "^", "$", "\", ".", "*", "+",
|
|
* "?", "(", ")", "[", "]", "{", "}", and "|" in `string`.
|
|
*
|
|
* @since 3.0.0
|
|
* @category String
|
|
* @param {string} [string=''] The string to escape.
|
|
* @returns {string} Returns the escaped string.
|
|
* @see escape, escapeRegExp, unescape
|
|
* @example
|
|
*
|
|
* escapeRegExp('[lodash](https://lodash.com/)')
|
|
* // => '\[lodash\]\(https://lodash\.com/\)'
|
|
*/
|
|
function escapeRegExp(string) {
|
|
return (string && reHasRegExpChar.test(string))
|
|
? string.replace(reRegExpChar, '\\$&')
|
|
: (string || '')
|
|
}
|
|
|
|
export default escapeRegExp
|