diff --git a/packages/next/shared/lib/escape-regexp.ts b/packages/next/shared/lib/escape-regexp.ts index 4d009f53b6f0a4d..a73cce0f21c9267 100644 --- a/packages/next/shared/lib/escape-regexp.ts +++ b/packages/next/shared/lib/escape-regexp.ts @@ -1,4 +1,11 @@ -// regexp from https://github.com/sindresorhus/escape-string-regexp +// regexp is based on https://github.com/sindresorhus/escape-string-regexp +const reHasRegExp = /[|\\{}()[\]^$+*?.-]/ +const reReplaceRegExp = /[|\\{}()[\]^$+*?.-]/g + export function escapeStringRegexp(str: string) { - return str.replace(/[|\\{}()[\]^$+*?.-]/g, '\\$&') + // see also: https://github.com/lodash/lodash/blob/2da024c3b4f9947a48517639de7560457cd4ec6c/escapeRegExp.js#L23 + if (reHasRegExp.test(str)) { + return str.replace(reReplaceRegExp, '\\$&') + } + return str }