JavaScript RegExp constructor Property

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Instance property

What You’ll Learn

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

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.

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.

📝 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

⚡ Quick Reference

GoalCode
Read constructorregex.constructor
Confirm RegExp typeregex.constructor === RegExp
Preferred type checkregex instanceof RegExp
Build another regexnew regex.constructor("pat", "g")
Clearer rebuildnew RegExp("pat", "g")
Fake regex object{ source: "x" }.constructor === Object

📋 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

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.

JavaScript
const regex = /hello/i;

console.log(regex.constructor);           // [Function: RegExp]
console.log(regex.constructor === RegExp); // true
Try It Yourself

How It Works

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.

JavaScript
const regex = new RegExp("\\d+", "g");
const plain = { source: "fake", flags: "g" };

console.log(regex.constructor === RegExp);  // true
console.log(plain.constructor === RegExp);  // false
console.log(plain.constructor === Object);  // true
Try It Yourself

How It Works

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.

JavaScript
const regex = /test/;

console.log(regex instanceof RegExp);     // true
console.log(regex.constructor === RegExp); // true

function isRegExp(value) {
  return value instanceof RegExp;
}
Try It Yourself

How It Works

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
Try It Yourself

How It Works

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.
Try It Yourself

How It Works

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.

🚀 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.
  • Reject impostors — plain objects mimicking source/flags fail constructor checks.
  • 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.

📝 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.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
RegExp.constructor Excellent

Bottom line: Safe everywhere. Use instanceof RegExp for type checks in everyday code.

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.

💡 Best Practices

✅ Do

  • Use instanceof RegExp for parameter validation
  • Log regex.constructor when exploring in DevTools
  • Pair type checks with meaningful error messages
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about RegExp constructor

Type identity for regular expression objects.

5
Core concepts
🔢 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.

Continue to RegExp exec()

Learn how to execute a regex and read capture groups from the result.

exec() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful