1
|
'use strict';
|
2
|
|
3
|
var has = require('has');
|
4
|
var regexExec = RegExp.prototype.exec;
|
5
|
var gOPD = Object.getOwnPropertyDescriptor;
|
6
|
|
7
|
var tryRegexExecCall = function tryRegexExec(value) {
|
8
|
try {
|
9
|
var lastIndex = value.lastIndex;
|
10
|
value.lastIndex = 0; // eslint-disable-line no-param-reassign
|
11
|
|
12
|
regexExec.call(value);
|
13
|
return true;
|
14
|
} catch (e) {
|
15
|
return false;
|
16
|
} finally {
|
17
|
value.lastIndex = lastIndex; // eslint-disable-line no-param-reassign
|
18
|
}
|
19
|
};
|
20
|
var toStr = Object.prototype.toString;
|
21
|
var regexClass = '[object RegExp]';
|
22
|
var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol';
|
23
|
|
24
|
module.exports = function isRegex(value) {
|
25
|
if (!value || typeof value !== 'object') {
|
26
|
return false;
|
27
|
}
|
28
|
if (!hasToStringTag) {
|
29
|
return toStr.call(value) === regexClass;
|
30
|
}
|
31
|
|
32
|
var descriptor = gOPD(value, 'lastIndex');
|
33
|
var hasLastIndexDataProperty = descriptor && has(descriptor, 'value');
|
34
|
if (!hasLastIndexDataProperty) {
|
35
|
return false;
|
36
|
}
|
37
|
|
38
|
return tryRegexExecCall(value);
|
39
|
};
|