2022-04-16 07:42:10 +02:00
|
|
|
// regexp is based on https://github.com/sindresorhus/escape-string-regexp
|
|
|
|
const reHasRegExp = /[|\\{}()[\]^$+*?.-]/
|
|
|
|
const reReplaceRegExp = /[|\\{}()[\]^$+*?.-]/g
|
|
|
|
|
2022-01-03 18:41:50 +01:00
|
|
|
export function escapeStringRegexp(str: string) {
|
2022-04-16 07:42:10 +02:00
|
|
|
// see also: https://github.com/lodash/lodash/blob/2da024c3b4f9947a48517639de7560457cd4ec6c/escapeRegExp.js#L23
|
|
|
|
if (reHasRegExp.test(str)) {
|
|
|
|
return str.replace(reReplaceRegExp, '\\$&')
|
|
|
|
}
|
|
|
|
return str
|
2022-01-03 18:41:50 +01:00
|
|
|
}
|