• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3var has = require('has');
4var regexExec = RegExp.prototype.exec;
5var gOPD = Object.getOwnPropertyDescriptor;
6
7var tryRegexExecCall = function tryRegexExec(value) {
8	try {
9		var lastIndex = value.lastIndex;
10		value.lastIndex = 0;
11
12		regexExec.call(value);
13		return true;
14	} catch (e) {
15		return false;
16	} finally {
17		value.lastIndex = lastIndex;
18	}
19};
20var toStr = Object.prototype.toString;
21var regexClass = '[object RegExp]';
22var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol';
23
24module.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};
40