Every RegExp object exposes a constructor property pointing at the built-in RegExp function. This guide shows how to read it, use it for type checks, compare it with instanceof, and know when simpler patterns are better.
01
Property
regex.constructor
02
Points to
RegExp
03
Type check
=== RegExp
04
instanceof
Preferred
05
Rebuild
new r.constructor()
06
Literal vs new
Same result
Fundamentals
Introduction
When you write /hello/i or new RegExp("hello", "i"), JavaScript builds a RegExp object. Like arrays and dates, that object carries a constructor property inherited from RegExp.prototype.
The property answers a simple question: which function created this object? For normal regexes the answer is RegExp. That is useful when you validate inputs, debug in the console, or read library code that inspects object types.
Concept
Understanding the constructor Property
The constructor property on a RegExp instance is a reference to the RegExp function itself:
Read-only in practice — every standard RegExp reports RegExp unless code manually overrides the property (rare and discouraged).
Same for literals and new — /abc/.constructor and new RegExp("abc").constructor both equal RegExp.
Not the pattern — the pattern lives in source and flags; constructor identifies the object type, not its contents.
Callable again — because it is the RegExp function, you can invoke new regex.constructor(pattern, flags), though direct new RegExp(...) reads better.
💡
Beginner Tip
Do not confuse regex.constructor (a property on one object) with the RegExp constructor you call to create regexes. The property simply points back to that same constructor function.
Foundation
📝 Syntax
JavaScript
regex.constructor
What it returns
The RegExp function for genuine RegExp instances.
Another constructor (such as Object) for plain objects that merely look like regexes.
Common checks
JavaScript
regex.constructor === RegExp // type hint
regex instanceof RegExp // preferred check
typeof regex === "object" && regex instanceof RegExp
Cheat Sheet
⚡ Quick Reference
Goal
Code
Read constructor
regex.constructor
Confirm RegExp type
regex.constructor === RegExp
Preferred type check
regex instanceof RegExp
Build another regex
new regex.constructor("pat", "g")
Clearer rebuild
new RegExp("pat", "g")
Fake regex object
{ source: "x" }.constructor === Object
Compare
📋 constructor vs instanceof
Both help identify RegExp objects, but they are not interchangeable in every scenario.
constructor
=== RegExp
Direct reference
instanceof
instanceof RegExp
Prototype chain
Override risk
constructor can lie
Rare edge case
Daily code
use instanceof
Best default
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it links. All examples use small RegExp objects you can inspect immediately.
📚 Getting Started
See what constructor returns on a literal regex.
Example 1 — Read the Constructor Property
Log the function behind a case-insensitive literal.
The engine stores a link from each instance back to the constructor function that built it. For regex literals, that is always the global RegExp function.
📈 Practical Patterns
Type checks, comparisons, and safe helpers.
Example 2 — Distinguish RegExp from Plain Objects
A fake object with source and flags is not a real regex.
Shape alone does not make a RegExp. Only objects created by the RegExp machinery carry RegExp as their constructor and inherit methods like test() and exec().
Example 3 — Compare with instanceof RegExp
For everyday validation, instanceof is the idiomatic choice.
instanceof walks the prototype chain. On a normal regex both checks agree; in advanced scenarios (cross-realm objects, overridden constructors) instanceof is usually more reliable.
Example 4 — Create a Regex via constructor
You can call the constructor property like a factory—though direct syntax is clearer.
JavaScript
const template = /placeholder/;
const built = new template.constructor("\\w+", "g");
console.log(built.source); // \w+
console.log(built.flags); // g
console.log(built.test("abc123")); // true
template.constructor is the RegExp function, so new template.constructor(...) behaves like new RegExp(...). Generic library code occasionally uses this pattern when the exact constructor must stay dynamic.
Example 5 — Guard Before Calling test()
Validate that a parameter is a RegExp before running pattern logic.
JavaScript
function runMatch(input, pattern) {
if (!(pattern instanceof RegExp)) {
return "Expected a RegExp instance.";
}
return pattern.test(input) ? "Match" : "No match";
}
console.log(runMatch("hello", /hel/)); // Match
console.log(runMatch("hello", "hel")); // Expected a RegExp instance.
Strings and plain objects do not expose working test() methods. An instanceof guard fails fast with a clear message instead of a confusing runtime error.
Applications
🚀 Common Use Cases
Console inspection — confirm an unknown value is a real RegExp while debugging.
API validation — ensure helper functions receive RegExp instances, not strings.
Library generics — clone or rebuild patterns via value.constructor when the type must stay dynamic.
Learning prototypes — connect RegExp instances back to the constructor function concept.
Documentation reading — recognize constructor checks in open-source regex utilities.
🧠 Where constructor Comes From
1
Create a RegExp
A literal or new RegExp(...) call runs the RegExp constructor.
/pattern/ or new
2
Link prototype
The instance inherits from RegExp.prototype, which defines constructor: RegExp.
Prototype chain
3
Read the property
regex.constructor resolves to the same RegExp function used at creation time.
Identity
4
🛠
Use in checks
Compare with RegExp, prefer instanceof, or call again to build new patterns.
Important
📝 Notes
constructor names the function that created the object—not the pattern string.
Prefer instanceof RegExp over constructor === RegExp in application code.
Literals and new RegExp() both yield RegExp as the constructor.
Cross-realm regexes (e.g., from an iframe) may fail instanceof checks in some environments.
Overriding constructor manually is possible but breaks expectations—avoid it.
For duck-typed checks, also verify methods like test exist if you cannot use instanceof.
Compatibility
Browser & Runtime Support
The constructor property on RegExp instances is part of standard JavaScript and has been supported since early ECMAScript editions.
✓ Baseline · ES1+
RegExp constructor
Supported in every modern browser and all Node.js versions. Works the same for literals and new RegExp().
99%Universal API
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
RegExp.constructorExcellent
Bottom line: Safe everywhere. Use instanceof RegExp for type checks in everyday code.
Wrap Up
Conclusion
The constructor property connects each RegExp instance back to the RegExp function. It is handy for exploration and generic utilities, while instanceof RegExp remains the clearest way to validate types in your own functions.
Next, learn RegExp exec() to run matches and capture groups, or review compile() for legacy pattern updates.
Use new RegExp(...) when building patterns dynamically
Read source and flags for pattern contents
❌ Don’t
Trust plain objects that only mimic regex shape
Override constructor on RegExp instances
Assume strings are regexes without conversion
Rely on constructor alone across iframe boundaries
Confuse constructor property with the pattern itself
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about RegExp constructor
Type identity for regular expression objects.
5
Core concepts
📝01
Property
regex.constructor
API
🔢02
Points to
RegExp fn.
Value
🔍03
instanceof
Preferred.
Check
🛠04
Rebuild
new r.constructor.
Pattern
✅05
Not pattern
use source.
Tip
❓ Frequently Asked Questions
Every RegExp instance inherits a constructor property that points to the RegExp function—the same function you call with new RegExp(pattern, flags). Reading regex.constructor tells you which built-in constructor created the object.
For ordinary RegExp objects created in the same realm, regex.constructor === RegExp is true. That confirms the object was built by the native RegExp constructor.
Prefer instanceof RegExp in modern code. It walks the prototype chain and is the usual pattern. constructor checks can fail if someone overrides the property or if you compare across iframes/realms.
Yes: new regex.constructor('pattern', 'flags') calls RegExp again. It works, but new RegExp('pattern', 'flags') or a literal /pattern/flags is clearer for readers.
No. Both /abc/ and new RegExp('abc') produce objects whose constructor property is RegExp.
A plain object with fake source and flags still has Object as its constructor, not RegExp. Combine instanceof RegExp with typeof checks when validating API inputs.
Did you know?
Every built-in prototype in JavaScript exposes a constructor property—arrays point to Array, dates to Date, and regexes to RegExp. The same idea applies across types.