If Than Else Regex Question Not Matching String

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

I have this payload.

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

Here is my regex101 test.

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

Thanks

Kyle

That's not how you use a regex to compare in Javascript... see Regular expressions - JavaScript | MDN

maybe .test( is what you need.

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

[{"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":[]}]

try Javaascript string.includes()

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

or in a switch node JSONata contains()

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

example

[{"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":[]}]

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

1 Like

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