# Switch Nodes - Regex

**URL:** <https://discourse.nodered.org/t/switch-nodes-regex/66628>\
**Category:** General\
**Created:** [19 August 2022 00:21 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628 "2022-08-19T00:21:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [19 August 2022 00:21 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/1 "2022-08-19T00:21:53Z")

</div>

Guys,

Trying to make some decisions on weather data that i am parsing and storing - it looks like this when stored

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

I am wanting to use a switch node to look for permutations with the word sunny in the description.

There does not seem to be a concise list of these on the website (typical Govt data feed !)

but so far i have seen

Sunny  
sunny  
mostly sunny  
occasional sun  
and others !

What i want to do is have a number of tests that will dictate how much charge i put into my battery system overnight - based on the proposed amount of sun (there will be other things that factor in - but this is one of the first tests)

I have setup a switch node so far as such

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

But there does not appear to be much (in the online help) about what is acceptable in the Regex field - i have tried various permutations but not getting far - i have checked online with various Regex parsers - its just it is difficult to work out what is acceptable to NR in this field ?

I am happy (wanting) for instance for the first field to match (case insensitive) on Sunny - but not to match on partly sunny or mostly sunny etc

ANy pointers please ?

Craig

---

<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:** [19 August 2022 03:54 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/2 "2022-08-19T03:54:11Z")

</div>

The original source data does not contain anything else useful ? like cloud coverage or something ?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [19 August 2022 04:03 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/3 "2022-08-19T04:03:19Z")

</div>

You can try something like this `^(?!(partly|mostly))(.*)?(sun|sunny)$`

Test Flow:

```auto
[{"id":"2867986b801a84ef","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Sunny","payloadType":"str","x":350,"y":2440,"wires":[["55f3dc4cf3381a83","fdb3a07b1b44fac6"]]},{"id":"06216f8bb3498d19","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"sunny","payloadType":"str","x":350,"y":2480,"wires":[["55f3dc4cf3381a83","fdb3a07b1b44fac6"]]},{"id":"35bc525d1b098bb6","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"mostly sunny","payloadType":"str","x":330,"y":2520,"wires":[["55f3dc4cf3381a83","fdb3a07b1b44fac6"]]},{"id":"ade067739695c3f8","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"occasional sun","payloadType":"str","x":320,"y":2560,"wires":[["55f3dc4cf3381a83","fdb3a07b1b44fac6"]]},{"id":"d40de17f9ae67bc8","type":"debug","z":"54efb553244c241f","name":"debug 27","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":700,"y":2520,"wires":[]},{"id":"794f2e8679226d6c","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"cloudy","payloadType":"str","x":350,"y":2620,"wires":[["55f3dc4cf3381a83","fdb3a07b1b44fac6"]]},{"id":"cb72167ecfe5c3f5","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"partly sunny","payloadType":"str","x":330,"y":2680,"wires":[["55f3dc4cf3381a83","fdb3a07b1b44fac6"]]},{"id":"55f3dc4cf3381a83","type":"function","z":"54efb553244c241f","name":"TEST","func":"\nconst regex =/^(?!(partly|mostly))(.*)?(sun|sunny)$/i;\n\nmsg.payload = regex.test(msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":530,"y":2520,"wires":[["d40de17f9ae67bc8"]]},{"id":"fdb3a07b1b44fac6","type":"switch","z":"54efb553244c241f","name":"","property":"payload","propertyType":"msg","rules":[{"t":"regex","v":"^(?!(partly|mostly))(.*)?(sun|sunny)$","vt":"str","case":true}],"checkall":"true","repair":false,"outputs":1,"x":570,"y":2640,"wires":[["71af1eb89582ad6f"]]},{"id":"71af1eb89582ad6f","type":"debug","z":"54efb553244c241f","name":"debug 28","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":700,"y":2640,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [19 August 2022 22:32 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/4 "2022-08-19T22:32:32Z")

</div>

> [@bakman2](#):
>
> The original source data does not contain anything else useful ? like cloud coverage or something ?

No unfortunately not - it is the most accurate - but sparse on detail !

> [@UnborN](#):
>
> You can try something like this `^(?!(partly|mostly))(.*)?(sun|sunny)$`

OK will give that a try - do you know if there is a list of what is acceptable though in the switch node regex field ?

Craig

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [20 August 2022 01:54 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/5 "2022-08-20T01:54:47Z")

</div>

> [@craigcurtin](#):
>
> do you know if there is a list of what is acceptable though in the switch node regex field ?

You can check out the [Javascript regex](https://www.w3schools.com/jsref/jsref_obj_regexp.asp) documentation - if you really want to go down that rabbit hole... just keep in mind that in Javascript the expressions are inside slashes, which are not used in Node-RED.

Another way is to use a JSONata expression that returns a true or false... for instance, here is a case-insensitive check for determining if the payload contains the string 'sun':

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

Put that expression in the Property field, and add output ports for the boolean values:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/5/75e9213949ffe8dd198b88728682d9c885302740.png)

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [20 August 2022 11:32 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/6 "2022-08-20T11:32:23Z")

</div>

I assume that the `i` in `\sun\i` is to ignore the case. So both sunny & SUNNY, etc would be 'true'.

---

<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:** [20 August 2022 15:46 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/7 "2022-08-20T15:46:02Z")

</div>

> No unfortunately not - it is the most accurate - but sparse on detail !

"occasional sun" could be the equivalent of "shower or two", doesn't sound very reliable.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [21 August 2022 01:04 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/8 "2022-08-21T01:04:01Z")

</div>

> [@bakman2](#):
>
> "occasional sun" could be the equivalent of "shower or two", doesn't sound very reliable.

Yep but they are the most accurate 12 hours - 24 hours out - so i can use this in the low solar months to enable precharging of my batteries overnight to enable peak shaving the next day.

As it gets closer i then find that 4-8 hours OpenWeathermap is good and their cloud forecasts are pretty accurate also.

Craig

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [21 August 2022 01:05 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/9 "2022-08-21T01:05:27Z")

</div>

> [@shrickus](#):
>
> Put that expression in the Property field, and add output ports for the boolean values:
> 
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/5/75e9213949ffe8dd198b88728682d9c885302740.png)

Thanks thats given me a good head start - and probably easier to decipher in a years time than the regex i would end up with

Craig

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [21 August 2022 01:18 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/10 "2022-08-21T01:18:50Z")

</div>

@shrickus OK what you had sort of works for me - but what i wanted to do was reference the property as a Global Context object - the reason being that i could then reference multiple values in the tests such as Sun (to match all the sun conditions), Cloud to match all the cloud conditions etc

Using your suggestion it appears i can only do a True/False test ? And so would have to cascade multiple switch nodes for that to work ?

Now i have done a search and @knolleary had responded that this was not the behaviour he was expecting - in this thread - [Problem with global json object variable and JSONata switch decision - #4 by knolleary](https://discourse.nodered.org/t/problem-with-global-json-object-variable-and-jsonata-switch-decision/27361/4)

i am wondering if that has been resolved now - does the switch node still ignore what is in the Property field if i reference a Global/Flow context variable and i have to build up the whole jsonata in the test rules - or is this now resolved ?

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

Craig

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [22 August 2022 14:59 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/11 "2022-08-22T14:59:11Z")

</div>

> [@craigcurtin](#):
>
> what i wanted to do was reference the property as a Global Context object - the reason being that i could then reference multiple values in the tests such as Sun (to match all the sun conditions), Cloud to match all the cloud conditions etc

In that case, I would set the switch property to be the global object value (assuming that works, of course), converted to a single case -- and then use a separate `contains` check for each output port:

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

It would be nice if the contains check had an `ignore case` option, like regex does... and you will probably have to tweak the order of your port checking if the incoming strings are like "Cloudy with some sun" :\*(

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [22 August 2022 22:40 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/12 "2022-08-22T22:40:37Z")

</div>

Good one thanks - i guess i would put a change node before this to get the Global context variable into the payload as i can not find a way to reference the Global context in the property field

Appreciate your time and will play with this and some other permutations and report back what works

Craig

---

<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:** [22 August 2022 23:16 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/13 "2022-08-22T23:16:49Z")

</div>

Contains with added ignore case sounds like a nice pull request if someone felt like contributing.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [23 August 2022 16:29 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/14 "2022-08-23T16:29:27Z")

</div>

> [@craigcurtin](#):
>
> can not find a way to reference the Global context in the property field

Does this property expression not get what you expect?

```auto
$globalContext('Weather.Today.Description').$uppercase()

```

Tbh, I never use context vars -- so IF that doesn't get the value you need, you could try getting the full Weather object from global context, and then just pull its fields using expression syntax, like so:

```auto
$globalContext('Weather').Today.Description.$uppercase()

```

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [23 August 2022 21:51 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/15 "2022-08-23T21:51:59Z")

</div>

I will play around with those suggestions thanks - i could not find a syntax that would work for me int he expression field - but will give those a go and let you know

Craig

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [23 August 2022 22:17 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/16 "2022-08-23T22:17:24Z")

</div>

> [@shrickus](#):
>
> `$globalContext('Weather').Today.Description.$uppercase()`

Nope tried both permutations and no good

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

Looks like i am going to have to cascade a few switch nodes together or do it in a function block

thanks for trying to help out

Craig

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [23 August 2022 22:49 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/17 "2022-08-23T22:49:05Z")

</div>

I don't think there is an issue with the switch property expression -- the problem is that your regex does not match the string stored in your global context... Is there a reason you are using such a complex regex?

Notice how the context var ends with a period `.` ? But your regex states that the value must end with either 'sun' or 'sunny' -- which it does not. Either take the `^` and `$` anchors off the regex so it can match anywhere in the text string, OR use the `contains` operator instead of a regex, as I tried to show previously.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [23 August 2022 23:29 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/18 "2022-08-23T23:29:22Z")

</div>

OK will try to mess around - i added that complex regex in (another member earlier in the thread posted that up and a test flow that seemed to work with inject nodes for testing) - i then went to the contains - but will step through it all again and just confirm and report back

Craig

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [23 August 2022 23:33 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/19 "2022-08-23T23:33:15Z")

</div>

Yep looks like you were right - went back to a simple contains and put SUN in uppercase and it is matching now - now i will just need to code up all the edge cases

Thanks for hanging in and helping out with this one

Craig

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [6 September 2022 23:33 UTC](https://discourse.nodered.org/t/switch-nodes-regex/66628/20 "2022-09-06T23:33:19Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
