From dad035d56100aefba2779ea5f3dbeb2f440d70cc Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 23 Dec 2018 21:51:53 -0600 Subject: [PATCH] Extract escapeRegExp(). --- src/util/escape-regexp.js | 17 +++++++++++++++++ src/util/is-native.js | 6 +++--- 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 src/util/escape-regexp.js diff --git a/src/util/escape-regexp.js b/src/util/escape-regexp.js new file mode 100644 index 000000000..08e811ea1 --- /dev/null +++ b/src/util/escape-regexp.js @@ -0,0 +1,17 @@ +import shared from "../shared.js" + +function init() { + const specialCharRegExp = /[\\^$.*+?()[\]{}|]/g + + function escapeRegExp(string) { + return typeof string === "string" + ? string.replace(specialCharRegExp, "\\$&") + : "" + } + + return escapeRegExp +} + +export default shared.inited + ? shared.module.utilEscapeRegExp + : shared.module.utilEscapeRegExp = init() diff --git a/src/util/is-native.js b/src/util/is-native.js index ef229fb12..bc18e1253 100644 --- a/src/util/is-native.js +++ b/src/util/is-native.js @@ -1,3 +1,4 @@ +import escapeRegExp from "./escape-regexp.js" import isProxy from "./is-proxy.js" import shared from "../shared.js" @@ -10,11 +11,10 @@ function init() { // compare other native methods against. Escape special RegExp characters // and remove method identifiers before converting the template to a regexp. const markerRegExp = /toString|(function ).*?(?=\\\()/g - const specialCharRegExp = /[\\^$.*+?()[\]{}|]/g + const nativeRegExp = RegExp( "^" + - toString.call(toString) - .replace(specialCharRegExp, "\\$&") + escapeRegExp(toString.call(toString)) .replace(markerRegExp, "$1.*?") + "$" )