# Is \`RegExp(…)\` really equivalent to \`new RegExp(…)\`?

**URL:** <https://es.discourse.group/t/is-regexp-really-equivalent-to-new-regexp/1637>\
**Category:** Spec Reading\
**Created:** [March 15, 2023, 6:31am UTC](https://es.discourse.group/t/is-regexp-really-equivalent-to-new-regexp/1637 "2023-03-15T06:31:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andersk](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/andersk/32/1671_2.png) [@andersk](https://es.discourse.group/u/andersk)\
**Post date:** [March 15, 2023, 6:31am UTC](https://es.discourse.group/t/is-regexp-really-equivalent-to-new-regexp/1637/1 "2023-03-15T06:31:53Z")

</div>

According to [§22.2.4 The RegExp Constructor](https://tc39.es/ecma262/multipage/text-processing.html#sec-regexp-constructor):

> Thus the function call `RegExp(…)` is equivalent to the object creation expression `new RegExp(…)` with the same arguments.

But empirically (in at least JavaScriptCore, SpiderMonkey, and V8), these calls are not equivalent:

```javascript
> r = /a/;
/a/
> RegExp(r) === r
true
> new RegExp(r) === r
false

```

and looking at the steps in §22.2.4.1, it should only be possible for the `RegExp` constructor to return its argument unchanged (2.b.ii) when NewTarget is **undefined** , i.e. it was called as `RegExp(…)` rather than `new RegExp(…)`.

Does the equivalence claim need to be corrected? It seems to have motivated an [incorrect optimization](https://github.com/swc-project/swc/issues/6941) in the SWC minifier.

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [March 15, 2023, 9:49am UTC](https://es.discourse.group/t/is-regexp-really-equivalent-to-new-regexp/1637/2 "2023-03-15T09:49:41Z")

</div>

Yep, agreed this looks incorrect. Could you raise an issue? [Issues · tc39/ecma262 · GitHub](https://github.com/tc39/ecma262/issues)

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [March 16, 2023, 7:51pm UTC](https://es.discourse.group/t/is-regexp-really-equivalent-to-new-regexp/1637/3 "2023-03-16T19:51:15Z")

</div>

Issue for reference: [Claimed equivalence between `RegExp(…)` and `new RegExp(…)` doesn’t hold · Issue #3028 · tc39/ecma262 · GitHub](https://github.com/tc39/ecma262/issues/3028)
