# Possible with Regex?

**URL:** <https://discourse.nodered.org/t/possible-with-regex/19672>\
**Category:** General\
**Created:** [28 December 2019 09:22 UTC](https://discourse.nodered.org/t/possible-with-regex/19672 "2019-12-28T09:22:54Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [28 December 2019 09:22 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/1 "2019-12-28T09:22:54Z")

</div>

Hey guys,

I never used regex expressions and only saw one in a Youtube guide where someone used one. Now I have a smart switch with 6 different states and I'm hoping I can get something done with regex expression inside a Switch Node.

If I would use a function node it would look like this:

```auto
var event = msg.payload;

if (event==1002) {
return [msg, null, null, null];
} else if (event==2002) {
return [null, msg, null, null];
} else if (event==1001 || event==1003) {
return [null, null, msg, null];
} else if (event==2001 || event==2003) {
return [null, null, null, msg];
}

```

I hope there is a solution for the last two if's.  
Can someone help me out?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [28 December 2019 10:00 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/2 "2019-12-28T10:00:26Z")

</div>

What does the input payload look like ?

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [28 December 2019 10:01 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/3 "2019-12-28T10:01:34Z")

</div>

only the number 1001 to 1003 or 2001 to 2003 only one of these numbers 🙂

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [28 December 2019 10:19 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/4 "2019-12-28T10:19:44Z")

</div>

I made my flow complete for my DimmerSwitch with a dimming flow. I only have to decide if the payload is 1002 || 2002 and otherwise. But how can I get an "if statement" into a regex that decides if 1002 or 2002 is the msg.payload.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [28 December 2019 10:24 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/5 "2019-12-28T10:24:44Z")

</div>

If you really want to use a regex, then something like `^(1002|2002)$` should do it.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [28 December 2019 10:27 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/6 "2019-12-28T10:27:57Z")

</div>

Regex or jsonata would work too in a switch node:

 ![Screenshot 2019-12-28 at 11.27.22 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/634e0b24699618ed0d92ea4626ac710efbed1be0.jpeg)

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [28 December 2019 10:30 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/7 "2019-12-28T10:30:51Z")

</div>

Mhhh didn't work

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/8cb3738896c0825e9f9f058a41a51cd0165830c4.png)

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [28 December 2019 10:34 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/8 "2019-12-28T10:34:53Z")

</div>

that did the job 🙂

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [28 December 2019 10:39 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/9 "2019-12-28T10:39:58Z")

</div>

It didn't work because you have selected the 'jsonata expression' type, which isn't a regex.

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [28 December 2019 10:45 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/10 "2019-12-28T10:45:41Z")

</div>

> [@knolleary](#):
>
> ^(1002|2002)$

ah lol 😃

thank you! did the job as well ofc

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/64f9394460e0d90d11e167277d205113f9b24977.png)

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [29 December 2019 15:37 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/11 "2019-12-29T15:37:38Z")

</div>

I have an other question belonging to this or even jsonata.

I have a msg.payload json object that looks like this:

`msg.payload = {status, color, switch, speed, effect}`

These objects should be split into 4 or 5 outputs (one for each).  
I got a solution that looks like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/8b04a0e7ab8591e6cee56451cc384d5e79daed8c.png)

Instead of using 4 or 5 change nodes that are looking like this:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/0d1b7418250859b38d6fea55312a78705fa86d50.png)

And then have an other switch node that checks if the payload isn't null... I thought there would be a nice solution with only 1 switch node that splits the payload depending on the object?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [29 December 2019 15:52 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/12 "2019-12-29T15:52:46Z")

</div>

Well you are using the switch node....albeit with 1 output.

You can use the switch node......or better yet, remove the switch nodes and remove the change nodes.  
in the dashboard nodes you can use `msg.payload.effect`, speed, switch etc directly as input.

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [29 December 2019 15:58 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/13 "2019-12-29T15:58:18Z")

</div>

I don't know exactly what you mean 😕

I know how to setup a switch node with 4 outputs but I don't know how to let it check if there is speed, switch and so on.

This doesn't work:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7ae63c7c9486bbcfa7286f00ea75164e71a958e7.png)

I have to split them because some of these payloads got numbers and some of these dashboard nodes use numbers as input 🙂

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [29 December 2019 16:16 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/14 "2019-12-29T16:16:01Z")

</div>

Can you show the payload ?

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [29 December 2019 16:17 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/15 "2019-12-29T16:17:06Z")

</div>

That's how it looks like if use function node, but I want to avoid it.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/702a0d792c9ae9b04c67a98569960280a7051723.png)

Here is an example payload:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/6a248d789f0a001016dc00d4b3e12be184e42d49.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [29 December 2019 16:29 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/16 "2019-12-29T16:29:36Z")

</div>

2 example flows in 1:

```auto
[{"id":"39f2942b.6e0054","type":"inject","z":"ac3a9890.7ebed","name":"","topic":"","payload":"{\"speed\":100,\"switch\":true,\"color\":{\"r\":255,\"g\":255,\"b\":255}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":458,"y":528,"wires":[["e88c6dd9.ad2dd","b287ddc8.5a86d8"]]},{"id":"e88c6dd9.ad2dd","type":"function","z":"ac3a9890.7ebed","name":"","func":"m = msg.payload\n\nspeed = {payload:m.speed}\nsw = {payload:m.switch}\ncolor = {payload:m.color}\n\nreturn [speed,sw,color]","outputs":3,"noerr":0,"x":626,"y":600,"wires":[["8fcc88db.bd968"],["5cc4bfc4.cc903"],["c0e85942.2a48e"]]},{"id":"b287ddc8.5a86d8","type":"switch","z":"ac3a9890.7ebed","name":"","property":"payload","propertyType":"msg","rules":[{"t":"hask","v":"speed","vt":"str"},{"t":"hask","v":"switch","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":626,"y":456,"wires":[["e0230e77.2efa6"],["1a300a87.85e855"]]},{"id":"8fcc88db.bd968","type":"debug","z":"ac3a9890.7ebed","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":814,"y":552,"wires":[]},{"id":"5cc4bfc4.cc903","type":"debug","z":"ac3a9890.7ebed","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":814,"y":600,"wires":[]},{"id":"c0e85942.2a48e","type":"debug","z":"ac3a9890.7ebed","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":814,"y":648,"wires":[]},{"id":"e0230e77.2efa6","type":"change","z":"ac3a9890.7ebed","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.speed","tot":"msg"},{"t":"delete","p":"payload.switch","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":432,"wires":[["247d4b6b.bb9ac4"]]},{"id":"1a300a87.85e855","type":"change","z":"ac3a9890.7ebed","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.switch","tot":"msg"},{"t":"delete","p":"payload.speed","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":480,"wires":[["99a9d3ed.38ad08"]]},{"id":"247d4b6b.bb9ac4","type":"debug","z":"ac3a9890.7ebed","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":982,"y":432,"wires":[]},{"id":"99a9d3ed.38ad08","type":"debug","z":"ac3a9890.7ebed","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":982,"y":480,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [29 December 2019 16:40 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/17 "2019-12-29T16:40:24Z")

</div>

> [@bakman2](#):
>
> 2 example flows in 1:

thank you so far, will take a look at this after launch \<3 But I think it would be a solution for my complex shit 😃

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [29 December 2019 18:10 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/18 "2019-12-29T18:10:42Z")

</div>

Ok tryed your flow. I forgot to mention that it is also possible to send a payload with just one of these objects. If the ESP does a fresh boot it will send all 4 objects with the msg.payload and if I just change one of these settings it will only sends one of these objects. There are some special effects that will send 3 of 4 and so on.

Tryed the method with your function node and I'm stuck at this point where I could declare a variable with an other variable and if the other variable is undefined it will set an other value. But this didn't work ☹

All of these Values of my dashboard nodes will be saved in flow variables. So I could send the saved value instead of an empty one.

```auto
var m = msg.payload,
    effect = {payload: m.effect} || {payload: flow.get('TEST_Switch_Desk_Effect')},
    speed = {payload: m.speed} || {payload: flow.get('TEST_Switch_Desk_Speed')},
    color = {payload: m.color} || {payload: flow.get('TEST_Switch_Desk_Color')},
    sw = {payload: m.switch} || {payload: flow.get('TEST_Switch_Desk')};
    
return [effect, speed, color, sw];

```

This will always lead to undefined payloads with the debug node ☹

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [29 December 2019 18:28 UTC](https://discourse.nodered.org/t/possible-with-regex/19672/19 "2019-12-29T18:28:17Z")

</div>

Maybe I got it now, think it is not the smartest solution but it works:

```auto
var m = msg.payload,
    effect = m.effect,
    speed = m.speed,
    color = m.color,
    sw = m.switch;

if (effect === undefined) {effect = {payload: flow.get('TEST_Switch_Desk_Effect')};}
else {effect = {payload: effect};}

if (speed === undefined) {speed = {payload: flow.get('TEST_Switch_Desk_Speed')};}
else {speed = {payload: speed};}

if (color === undefined) {color = {payload: flow.get('TEST_Switch_Desk_Color')};}
else {color = {payload: color};}

if (sw === undefined) {sw = {payload: flow.get('TEST_Switch_Desk')};}
else {sw = {payload: sw};}

return [effect, speed, color, sw];

```
