# Question: Add RegExp as \`case\` statement in \`switch\`

**URL:** <https://es.discourse.group/t/question-add-regexp-as-case-statement-in-switch/2212>\
**Category:** I have questions\
**Created:** [November 13, 2024, 3:25pm UTC](https://es.discourse.group/t/question-add-regexp-as-case-statement-in-switch/2212 "2024-11-13T15:25:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kalisjoshua](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/kalisjoshua/32/2295_2.png) [@kalisjoshua](https://es.discourse.group/u/kalisjoshua)\
**Post date:** [November 13, 2024, 3:25pm UTC](https://es.discourse.group/t/question-add-regexp-as-case-statement-in-switch/2212/1 "2024-11-13T15:25:40Z")

</div>

This might be a short discussion. Is there a reason not to add/allow a regex as a value for a case in a swtich?

```javascript
function doStuff(arg) {
  switch (arg) {
    case /abc/:
      // do something
      break; // or return

    case /xyz/:
      // do something else
      break; // or return

    // other cases with other regexs

    default:
      // do other things
      break; // or return
  }
}

doStuff('abc'); // matches /abc/
doStuff('xyz'); // matches /xyz/
doStuff(undefined); // matches default (maybe)

```

---

<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:** [November 13, 2024, 3:30pm UTC](https://es.discourse.group/t/question-add-regexp-as-case-statement-in-switch/2212/2 "2024-11-13T15:30:16Z")

</div>

One reason could be that this is already "in the works" as part of [GitHub - tc39/proposal-pattern-matching: Pattern matching syntax for ECMAScript](https://github.com/tc39/proposal-pattern-matching#built-in-custom-matchers)

---

<div class="post-metadata">

**Author:** ![hax](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/hax/32/662_2.png) [@hax](https://es.discourse.group/u/hax)\
**Post date:** [November 14, 2024, 7:36am UTC](https://es.discourse.group/t/question-add-regexp-as-case-statement-in-switch/2212/3 "2024-11-14T07:36:19Z")

</div>

It has been valid syntax since ES3, though it's useless (because `case /xyz/:` never matches). We can't change the semantics (theoretically possible, but very unlikely in practice). So the only option is to introduce a new feature, aka. the pattern matching proposal.

---

<div class="post-metadata">

**Author:** ![kalisjoshua](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/kalisjoshua/32/2295_2.png) [@kalisjoshua](https://es.discourse.group/u/kalisjoshua)\
**Post date:** [November 14, 2024, 3:15pm UTC](https://es.discourse.group/t/question-add-regexp-as-case-statement-in-switch/2212/4 "2024-11-14T15:15:19Z")

</div>

I guess I was a little unclear. I was intending to ask about why we wouldn't want to add it as a feature in a future version. Secondly, my intention was for it to work by actually testing the pattern against the value provided in the `switch(value)` opening statement. So it would behave more like this:

```javascript
function doStuff(arg = '') {
  switch (true) {
    case /abc/.test(arg):
      return 'hello'

    case /xyz/.test(arg):
      return 'world';

    // other cases with other regexs

    default:
      // do other things
      return 'nope.'
  }
}

doStuff('abc'); // matches /abc/
doStuff('xyz'); // matches /xyz/
doStuff(undefined); // matches default (maybe)

```

---

<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:** [November 14, 2024, 4:14pm UTC](https://es.discourse.group/t/question-add-regexp-as-case-statement-in-switch/2212/5 "2024-11-14T16:14:49Z")

</div>

One reason it might not get added to `switch` in a future version of JavaScript is that it might be part of [GitHub - tc39/proposal-pattern-matching: Pattern matching syntax for ECMAScript](https://github.com/tc39/proposal-pattern-matching), which allows switch to remain as it is. And for code wanting the more advanced features they would _switch_ to `match`

---

<div class="post-metadata">

**Author:** ![kalisjoshua](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/kalisjoshua/32/2295_2.png) [@kalisjoshua](https://es.discourse.group/u/kalisjoshua)\
**Post date:** [November 14, 2024, 4:22pm UTC](https://es.discourse.group/t/question-add-regexp-as-case-statement-in-switch/2212/6 "2024-11-14T16:22:47Z")

</div>

That's valid. I wasn't aware of `match` coming so thank you for that. I'll have to check it out.
