heyujie
2021-05-24 4885600ecc369aa2e30a65de8dd7a410f13c34df
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"use strict";
 
var isToStringTagSupported = require("../lib/is-to-string-tag-supported")
  , isPrototype            = require("../prototype/is");
 
var regExpTest = RegExp.prototype.test
  , objectToString = Object.prototype.toString
  , objectTaggedString = objectToString.call(/a/);
 
module.exports = function (value) {
    if (!value) return false;
 
    // Sanity check (reject objects which do not expose common RegExp interface)
    if (!hasOwnProperty.call(value, "lastIndex")) return false;
    try {
        if (typeof value.lastIndex !== "number") return false;
        if (typeof value.test !== "function") return false;
        if (typeof value.exec !== "function") return false;
    } catch (error) {
        return false;
    }
 
    // Ensure its native RegExp object (has [[RegExpMatcher]] slot)
    if (isToStringTagSupported && typeof value[Symbol.toStringTag] === "string") {
        // Edge case (possibly a regExp with custom Symbol.toStringTag)
        try {
            var lastIndex = value.lastIndex;
            regExpTest.call(value, "");
            if (value.lastIndex !== lastIndex) value.lastIndex = lastIndex;
            return true;
        } catch (error) {
            return false;
        }
    }
    if (objectToString.call(value) !== objectTaggedString) return false;
    return !isPrototype(value);
};