# Switch Node: Pass on regexp matches with the msg

**URL:** <https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936>\
**Category:** Feature Requests\
**Tags:** switch-node\
**Created:** [12 December 2025 17:13 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936 "2025-12-12T17:13:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [12 December 2025 17:13 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/1 "2025-12-12T17:13:35Z")

</div>

Hi there,

I have this idea .... would it be possible to pass on the matches of a regexp in a switch node?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b08339340fa1eca6d062118dd49d5acbdfe73a2c.png)

The groupings in my regexp could be attached to the msg object and then used down the line ... instead of me adding a new change/function node to do the same regexp.

So the msg object would come out with `msg.match = [...]` or whatever the match object is ....

I know: this does go against the switch node not modifying the msg object but just this once ... plllleeeeeaaaasssssseeeeee!

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [12 December 2025 17:49 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/2 "2025-12-12T17:49:53Z")

</div>

ooo - like that ! 👍

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [12 December 2025 18:00 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/3 "2025-12-12T18:00:20Z")

</div>

> [@dceejay](#):
>
> ooo - like that

Damn ... I was hoping you'd post "check this magic checkbox and it works" ... ☹

😉

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [12 December 2025 18:52 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/4 "2025-12-12T18:52:17Z")

</div>

it's an invisible checkbox....

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [12 December 2025 20:19 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/5 "2025-12-12T20:19:33Z")

</div>

So - having a quick look - of course javascript regex doesn't return a standard object - so would we want to return a non-standard object or re-map it to some other shape.  
Also currently we just do a match so it stops on first hit - would need to change it to matchAll and use /g which could have performance impacts I guess.

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [12 December 2025 20:32 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/6 "2025-12-12T20:32:40Z")

</div>

> [@dceejay](#):
>
> have performance impacts I guess

looking at the screenshot, i see space next to the "ignore case" option ... "add matches to msg" as an extra option so that existing performance is maintained.

So that would be the magic invisible-made-visible checkbox ...

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [12 December 2025 20:44 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/7 "2025-12-12T20:44:06Z")

</div>

Do you have any standalone examples I could play with ? (so we can looks at possible output format)

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [12 December 2025 21:00 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/8 "2025-12-12T21:00:53Z")

</div>

```auto
$match($$.url,/^https:\/\/github\.com\/([^\/]+)\/([^\/]+)\/blob\/([^\/]+)\/(.+)/)

```

that parses a github url - `https://github.com/gorenje/erlang-red/blob/main/src/nodes/bookkeepers/ered_node_disabled.erl` - What I get from that is owner, repo, revision and path - so four parts.

The jsonata result is:

```auto
{
  "match":"https://github.com/gorenje/erlang-red/blob/main/src/nodes/bookkeepers/ered_node_disabled.erl",
  "index":0,
  "groups":[
   "gorenje",
   "erlang-red",
   "main",
   "src/nodes/bookkeepers/ered_node_disabled.erl"
  ]
}

```

so keeping it similar would probably be optimal...

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [12 December 2025 21:36 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/9 "2025-12-12T21:36:38Z")

</div>

what does jsonata return for multiple matches in a string eg /[b][aio]/g with "bishbashbosh" ? Ie should it really return and array of matches each element of which is like above ?

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [12 December 2025 22:15 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/10 "2025-12-12T22:15:00Z")

</div>

Let me give an unpopular view...  
If you know how to use e a regex and return matches, and/or work with JSONata, you are no longer in low code zone.  
Then why not just use a function node with multiple output ports, and do anything you want?

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [12 December 2025 22:18 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/11 "2025-12-12T22:18:21Z")

</div>

> [@dceejay](#):
>
> /[b][aio]/g with "bishbashbosh"

```auto
[
  {"match":"bi","index":0,"groups":[]},
  {"match":"ba","index":4,"groups":[]},
  {"match":"bo","index":8,"groups":[]}
]

```

> [@omrid](#):
>
> If you know how to use e a regex and return matches, and/or work with JSONata, you are no longer in low code zone.

But a function node is even more knowledge. JSONata is well documented and easier to understand, JavaScript is definitely more complex hence knowing JSONata does not necessarily mean you know how to create JS code.

As for regex, that's just magic that you're either born with or not...

> [@omrid](#):
>
> function node with multiple output ports,

Sure, everything done in a flow can be done with a single function node but then I won't be using Node-RED ... the challenge lies in _not_ using the function node.

In this case, it's about not repeating the regexp in two different places.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [12 December 2025 22:25 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/12 "2025-12-12T22:25:39Z")

</div>

> [@omrid](#):
>
> Let me give an unpopular view...

Well I guess todays popular view would be just to throw the text at an LLM and ask it to sort it out for you 🙂 but yeah - this is just a Friday evening goof off at present. Function nodes is what I do now and 🤷 options.

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [12 December 2025 22:28 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/13 "2025-12-12T22:28:38Z")

</div>

> [@gregorius](#):
>
> But a function node is even more knowledge. JSONata is well documented and easier to understand

To each his own weapon, of course, but I beg to differ. JS is widely used, while JSONata is quite obscure to non-node-red users (and also introduces a performance hit on top of, well, JS which is what node-red is written with.

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [12 December 2025 22:48 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/14 "2025-12-12T22:48:40Z")

</div>

> [@gregorius](#):
>
> In this case, it's about not repeating the regexp in two different places

Why would it be replicating the regex? You just replace a switch node with a function node.  
A function node allows you even better reuse by setting the regex (or logical functions, which are more readable than regex magic) as global/flow context variables.

Make no mistake. I love node-red, and I love visual programming, but I see some people create flows which grow up to become huge & complex, and hard to understand & maintain. They could be simplified with some function nodes.

BTW, function nodes can also (where needed) ensure that multiple actions are executed as one transaction without race conditions between messages.

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [12 December 2025 22:49 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/15 "2025-12-12T22:49:33Z")

</div>

> [@omrid](#):
>
> JSONata is quite obscure to non-node-red users

Being focused on a single language makes everything a nail. I personally am a polyglot programmer: use the right language for the problem, don't make the problem fit into the language one knows.

Being that strange person who decided to replicate Node-RED in [Erlang](https://github.com/gorenje/erlang-red), I use a lot of JSONata because I've ported JSONata to [Erlang](https://github.com/gorenje/erlang-red-jsonata). Hence I prefer JSONata so that I can copy & paste my Node-RED flows into Erlang-Red and have them execute there - in Erlang.

So JSONata, JS, Erlang or Java, Python, Lisp and Schema ... Prolog or perhaps Bash - what's the difference, as long as the problem gets solved.

> [@dceejay](#):
>
> this is just a Friday evening goof off at present.

Just as an aside, I'm currently working with a large and confusing codebase. I'm trying to identify particularly complicate bits of code, so that they can be refactored. What I have done is began to visual the [Abstract Syntax Tree](https://www.erlang.org/doc/apps/syntax_tools/erl_syntax.html) (AST) as flows:

 ![Screenshot 2025-12-12 at 23.43.05](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/8/3831f10219d27f9737b60f189232089da789ea9a.jpeg)

what is shown are the branches of switch/case and if statements but using minified nodes (hence all the same size). That flow is a (part) visualisation of 20k lines of Erlang.

Why? Because I can visual 20k lines of code in a single flow:

 ![Screenshot 2025-12-12 at 23.46.59](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/5/951fc551712c8a1a17fd65b0b47a842caa4b8f90.jpeg)

(Note: bottom right is a case/switch statement with many branches ... hm?)

Now the fun bit is that this is a flow and I can send messages through this flow - so my representation of Erlang code is Node-RED and is executable. Eventually this NR code will generate the Erlang code again.

That's the kind of stuff I like to do.

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [12 December 2025 22:53 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/16 "2025-12-12T22:53:25Z")

</div>

> [@omrid](#):
>
> Make no mistake. I love node-red, and I love visual programming, but I see some people create flows which grow up to become huge & complex, and hard to understand & maintain. They could be simplified with some function nodes.

Yes but those are the same people that generate complicate text code. Sorry to say but coding requires discipline and reflection and refactoring. If you don't do these things, then visual or textual, the result will be the same.

> [@omrid](#):
>
> without race conditions between messages.

Hence you don't want state in your flows. Using context (flow, node or global) is an error - the only state is in the message object, therefore there wouldn't be a race condition to take care of.

Race conditions only occur if resources are shared.

For background on this, see [Flow-based Programming](https://jpaulm.github.io/fbp/) - Node RED is not flow based programming, it's inspired by FBP.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [12 December 2025 23:02 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/17 "2025-12-12T23:02:10Z")

</div>

Node-RED even gets a mention||

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [12 December 2025 23:40 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/18 "2025-12-12T23:40:21Z")

</div>

Yeah - we had a call with Paul Morrison way back when, after we discovered that we had re-invented FBP, and that he had been at IBM also.

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [13 December 2025 07:40 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/19 "2025-12-13T07:40:29Z")

</div>

> [@dceejay](#):
>
> re-invented FBP,

I'm going to quote you on that! My thinking of the lineage had been that Node-RED was influenced by Yahoo! Pipes and FBP but then it's just Yahoo! Pipes or did Nick and you not know of that either 😉

Speaking of lineage, apparently FBP influenced Unix PIpes - at least there is some discussion around this over at the [c2 wiki](https://wiki.c2.com/?FlowBasedProgramming). But that would then, for me, make a relative clear lineage: NothingnessOfGodlessness --\> FBP --\> Unix Pipes --\> Yahoo! Pipes --\> Node-RED which makes great story telling! Of course Paul Morrison was inspired by something and it would be nice to know what.

Can I take that heritage and be somewhat correct?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [13 December 2025 08:10 UTC](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936/20 "2025-12-13T08:10:39Z")

</div>

Why not do the regex using js or JSONata prior to the switch node, then use the switch node to check for matches

Seems adding extra complexity to the switch node is not really required here.

[Next page](https://discourse.nodered.org/t/switch-node-pass-on-regexp-matches-with-the-msg/99936.md?page=2)
