Back
extracted
is-regex
v1.1.3
MIT

isRegex

A vendorable is-regex bundle based on the 1.1.3 package line, with has-symbols and call-bound logic folded into one file for Node.js and Bun use.

Why copy this?

This page ships an is-regex bundle based on the older package line, with helper logic folded into a single vendorable file. The value is to keep the package's detection contract local without a deeper helper graph.

Native alternative: There is no direct native helper for the package's exact regexp-detection contract, especially around Symbol.toStringTag edge cases.

Note: Current is-regex releases pull in more helper packages. This snippet uses the 1.1.3 line as a simpler vendorable first pass.

This copy is your responsibility once you adopt it. It does not automatically receive upstream bug fixes or security updates.

Snippet
Copy-first distribution
Normalized
ESM / TS / normalized
Runtime: node, bun
/**
 * Derived from [email protected]
 * Rule-based normalized variant generated by this repository.
 * Preserve the upstream license and attribution notices when copying this file.
 */
/**
 * Derived from [email protected]
 * Rule-based normalized variant generated by this repository.
 * See THIRD_PARTY_NOTICES.md for upstream license and attribution details.
 */
"use strict";
var hasSymbols = function hasSymbols() {
  if (typeof Symbol !== "function" || typeof Object.getOwnPropertySymbols !== "function") {
    return false;
  }
  if (typeof Symbol.iterator === "symbol") {
    return true;
  }

  var obj = {};
  var sym = Symbol("test");
  var symObj = Object(sym);
  if (typeof sym === "string") {
    return false;
  }
  if (Object.prototype.toString.call(sym) !== "[object Symbol]") {
    return false;
  }
  if (Object.prototype.toString.call(symObj) !== "[object Symbol]") {
    return false;
  }

  var symVal = 42;
  obj[sym] = symVal;
  for (var _ in obj) {
    return false;
  }
  if (typeof Object.keys === "function" && Object.keys(obj).length !== 0) {
    return false;
  }
  if (
    typeof Object.getOwnPropertyNames === "function" &&
    Object.getOwnPropertyNames(obj).length !== 0
  ) {
    return false;
  }

  var syms = Object.getOwnPropertySymbols(obj);
  if (syms.length !== 1 || syms[0] !== sym) {
    return false;
  }
  if (!Object.prototype.propertyIsEnumerable.call(obj, sym)) {
    return false;
  }

  if (typeof Object.getOwnPropertyDescriptor === "function") {
    var descriptor = Object.getOwnPropertyDescriptor(obj, sym);
    if (descriptor.value !== symVal || descriptor.enumerable !== true) {
      return false;
    }
  }

  return true;
};

var hasToStringTag = hasSymbols() && !!Symbol.toStringTag;
var has = Function.call.bind(Object.prototype.hasOwnProperty);
var $exec = Function.call.bind(RegExp.prototype.exec);
var $toString = Function.call.bind(Object.prototype.toString);
var isRegexMarker;
var badStringifier;

if (hasToStringTag) {
  isRegexMarker = {};

  var throwRegexMarker = function () {
    throw isRegexMarker;
  };
  badStringifier = {
    toString: throwRegexMarker,
    valueOf: throwRegexMarker,
  };

  if (typeof Symbol.toPrimitive === "symbol") {
    badStringifier[Symbol.toPrimitive] = throwRegexMarker;
  }
}

var gOPD = Object.getOwnPropertyDescriptor;
var regexClass = "[object RegExp]";

export default hasToStringTag
  ? function isRegex(value) {
      if (!value || typeof value !== "object") {
        return false;
      }

      var descriptor = gOPD(value, "lastIndex");
      var hasLastIndexDataProperty = descriptor && has(descriptor, "value");
      if (!hasLastIndexDataProperty) {
        return false;
      }

      try {
        $exec(value, badStringifier);
      } catch (e) {
        return e === isRegexMarker;
      }
    }
  : function isRegex(value) {
      if (!value || (typeof value !== "object" && typeof value !== "function")) {
        return false;
      }

      return $toString(value) === regexClass;
    };
Variant note: Bundled into one file and then rule-based normalized for copy-paste into modern TS projects.
Transforms: module.exports-to-export-default, js-extension-to-ts-extension, bundled-upstream-helper
Validation: This normalized variant is intended to stay oxlint and oxfmt clean in this repo.
Raw
CJS / JS / raw
Runtime: node, bun
'use strict';

var hasSymbols = function hasSymbols() {
  if (typeof Symbol !== 'function' || typeof Object.getOwnPropertySymbols !== 'function') { return false; }
  if (typeof Symbol.iterator === 'symbol') { return true; }

  var obj = {};
  var sym = Symbol('test');
  var symObj = Object(sym);
  if (typeof sym === 'string') { return false; }
  if (Object.prototype.toString.call(sym) !== '[object Symbol]') { return false; }
  if (Object.prototype.toString.call(symObj) !== '[object Symbol]') { return false; }

  var symVal = 42;
  obj[sym] = symVal;
  for (var _ in obj) { return false; }
  if (typeof Object.keys === 'function' && Object.keys(obj).length !== 0) { return false; }
  if (typeof Object.getOwnPropertyNames === 'function' && Object.getOwnPropertyNames(obj).length !== 0) { return false; }

  var syms = Object.getOwnPropertySymbols(obj);
  if (syms.length !== 1 || syms[0] !== sym) { return false; }
  if (!Object.prototype.propertyIsEnumerable.call(obj, sym)) { return false; }

  if (typeof Object.getOwnPropertyDescriptor === 'function') {
    var descriptor = Object.getOwnPropertyDescriptor(obj, sym);
    if (descriptor.value !== symVal || descriptor.enumerable !== true) { return false; }
  }

  return true;
};

var hasToStringTag = hasSymbols() && !!Symbol.toStringTag;
var has = Function.call.bind(Object.prototype.hasOwnProperty);
var $exec = Function.call.bind(RegExp.prototype.exec);
var $toString = Function.call.bind(Object.prototype.toString);
var isRegexMarker;
var badStringifier;

if (hasToStringTag) {
  isRegexMarker = {};

  var throwRegexMarker = function () {
    throw isRegexMarker;
  };
  badStringifier = {
    toString: throwRegexMarker,
    valueOf: throwRegexMarker
  };

  if (typeof Symbol.toPrimitive === 'symbol') {
    badStringifier[Symbol.toPrimitive] = throwRegexMarker;
  }
}

var gOPD = Object.getOwnPropertyDescriptor;
var regexClass = '[object RegExp]';

module.exports = hasToStringTag
  ? function isRegex(value) {
    if (!value || typeof value !== 'object') {
      return false;
    }

    var descriptor = gOPD(value, 'lastIndex');
    var hasLastIndexDataProperty = descriptor && has(descriptor, 'value');
    if (!hasLastIndexDataProperty) {
      return false;
    }

    try {
      $exec(value, badStringifier);
    } catch (e) {
      return e === isRegexMarker;
    }
  }
  : function isRegex(value) {
    if (!value || (typeof value !== 'object' && typeof value !== 'function')) {
      return false;
    }

    return $toString(value) === regexClass;
  };
Variant note: Single-file CommonJS bundle preserving the package's detection logic without external helper packages.