Check if a value is a regular expression
Read the internal tag, which also works for values from another realm.
function isRegExp(value) { return Object.prototype.toString.call(value) === '[object RegExp]'; }
Usage
isRegExp(/^[a-z]+$/); // → true
isRegExp(new RegExp('^[a-z]+$')); // → true
isRegExp('^[a-z]+$'); // → false
Know the limits
- Works across realms (iframes, vm contexts), where instanceof RegExp fails. Symbol.toStringTag can spoof the tag.
- Never build a RegExp from untrusted text without escaping it: some patterns take long enough to stall a server (ReDoS).
The function above is the code the usage lines run. It does not validate its arguments; add checks where it receives untrusted input.
Source & maintenance
Based on Check if a value is a regular expression from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.
Documented as a reference with executed usage lines. Original MDX retained as inert source evidence.
Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.
Keep exploring
- Check whether all values are equal — Compare every item with the first using Object.is.
- Check whether an object is empty — Check for own enumerable string keys on a JSON object.
- Check whether an integer is even — Test parity for safe integers, including zero and negative values.