2026-05-31 13:54:31 +02:00

18 lines
575 B
JavaScript

//#region src/string/escapeRegExp.ts
/**
* Escapes the RegExp special characters "^", "$", "\\", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", and "|" in `str`.
*
* @param {string} str The string to escape.
* @returns {string} Returns the escaped string.
*
* @example
* import { escapeRegExp } from 'es-toolkit/string';
*
* escapeRegExp('[es-toolkit](https://es-toolkit.dev/)'); // returns '\[es-toolkit\]\(https://es-toolkit\.dev/\)'
*/
function escapeRegExp(str) {
return str.replace(/[\\^$.*+?()[\]{}|]/g, "\\$&");
}
//#endregion
exports.escapeRegExp = escapeRegExp;