# If Than Else Regex Question Not Matching String

**URL:** <https://discourse.nodered.org/t/if-than-else-regex-question-not-matching-string/56751>\
**Category:** General\
**Created:** [16 January 2022 15:15 UTC](https://discourse.nodered.org/t/if-than-else-regex-question-not-matching-string/56751 "2022-01-16T15:15:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aa0z](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@aa0z](https://discourse.nodered.org/u/aa0z)\
**Post date:** [16 January 2022 15:15 UTC](https://discourse.nodered.org/t/if-than-else-regex-question-not-matching-string/56751/1 "2022-01-16T15:15:16Z")

</div>

Need some help with a basic regex issue that I've been battling for a while.

I have this payload.

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

In my function node, I do NOT want to pass any message that contains the words QRT, caps do not matter. I've tried a switch node with a regex and contains with no luck so I'm back to a function node. I've plugged in my regex into an editor and verified the syntax is correct (or as far as I know).

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

Here is my regex101 test.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/4/24d32e39f316d2edd3be2d24c2959e6279d0eb54.png)

My function node is passes everything, which I do not want. Kinda at a roadblock here.

Thanks

Kyle

---

<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:** [16 January 2022 15:26 UTC](https://discourse.nodered.org/t/if-than-else-regex-question-not-matching-string/56751/2 "2022-01-16T15:26:01Z")

</div>

That's not how you use a regex to compare in Javascript... see [Regular expressions - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#using_regular_expressions_in_javascript)

maybe .test( is what you need.

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [16 January 2022 15:34 UTC](https://discourse.nodered.org/t/if-than-else-regex-question-not-matching-string/56751/3 "2022-01-16T15:34:30Z")

</div>

Use a switch node, much easier.  
All valid messages will be outputted on 1.  
Rest will be routed to output 2.

 ![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/9/994faaaa2217478e5cfd85833b7ff1a61ceb4673.png)

```auto
[{"id":"44a3cff3275f3259","type":"switch","z":"8b114306.e05788","name":"","property":"payload.comments","propertyType":"msg","rules":[{"t":"regex","v":"qrt","vt":"str","case":true},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":550,"y":240,"wires":[["be9a9d7ac76926b8"],["39c294828d430723"]]},{"id":"be9a9d7ac76926b8","type":"debug","z":"8b114306.e05788","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":930,"y":220,"wires":[]},{"id":"5a7f3a6bfc3f47b6","type":"inject","z":"8b114306.e05788","name":"","props":[{"p":"payload.comments","v":"qrT","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":330,"y":240,"wires":[["44a3cff3275f3259"]]},{"id":"39c294828d430723","type":"debug","z":"8b114306.e05788","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":930,"y":260,"wires":[]}]

```

---

<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:** [16 January 2022 16:03 UTC](https://discourse.nodered.org/t/if-than-else-regex-question-not-matching-string/56751/4 "2022-01-16T16:03:42Z")

</div>

try Javaascript `string.includes()`

```auto
if ( !msg.payload.comments.toLowerCase().includes("qrt")){
     msg = null;
}
return msg;

```

or in a switch node JSONata `contains()`

```auto
$contains( $lowercase($$.payload.comments), "qrt")

```

example

```auto
[{"id":"d25af463a7c58e7a","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload.comments","v":"tt qrt","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":3280,"wires":[["c7ff0a7d.2aa708","55c36b04.3065f4"]]},{"id":"c7ff0a7d.2aa708","type":"function","z":"c791cbc0.84f648","name":"","func":"if ( !msg.payload.comments.toLowerCase().includes(\"qrt\")){\n msg = null;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":3300,"wires":[["f46e3f30e8f2459c"]]},{"id":"55c36b04.3065f4","type":"switch","z":"c791cbc0.84f648","name":"","property":"$contains( $lowercase($$.payload.comments), \"qrt\")","propertyType":"jsonata","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":350,"y":3360,"wires":[["f46e3f30e8f2459c"]]},{"id":"a0200cdbd74d6e7f","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload.comments","v":"ttt urt","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":3320,"wires":[["c7ff0a7d.2aa708","55c36b04.3065f4"]]},{"id":"f46e3f30e8f2459c","type":"debug","z":"c791cbc0.84f648","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":680,"y":3300,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![aa0z](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@aa0z](https://discourse.nodered.org/u/aa0z)\
**Post date:** [16 January 2022 18:54 UTC](https://discourse.nodered.org/t/if-than-else-regex-question-not-matching-string/56751/5 "2022-01-16T18:54:34Z")

</div>

The switch node solved it. Thanks @edje11.

I thought when choosing regex expression in a switch node, it was looking for a regex expression and not just plain text to match. That's where I went wrong and started down the path of If Than Else.

Also looked at the javascript regex syntax. Thanks for the referral.

Thanks for the replies from everybody else. Still learning here....

Kyle

---

<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:** [17 March 2022 18:55 UTC](https://discourse.nodered.org/t/if-than-else-regex-question-not-matching-string/56751/6 "2022-03-17T18:55:21Z")

</div>

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