@newdash/newdash
Version:
javascript/typescript utility library
37 lines (36 loc) • 1.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.escapeRegExp = void 0;
/**
* Used to match `RegExp`
* [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns).
* @ignore
*/
const reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
/**
* @ignore
*/
const reHasRegExpChar = RegExp(reRegExpChar.source);
/**
* Escapes the `RegExp` special characters "^", "$", "\", ".", "*", "+",
* "?", "(", ")", "[", "]", "{", "}", and "|" in `string`.
*
* @since 5.12.0
* @category String
* @param string The string to escape.
* @returns string Returns the escaped string.
* @see [[escape]], [[escapeRegExp]], [[unescape]]
* @example
*
* ```js
* escapeRegExp('[lodash](https://lodash.com/)')
* // => '\[lodash\]\(https://lodash\.com/\)'
* ```
*/
function escapeRegExp(string) {
return (string && reHasRegExpChar.test(string))
? string.replace(reRegExpChar, "\\$&")
: (string || "");
}
exports.escapeRegExp = escapeRegExp;
exports.default = escapeRegExp;