Regex match in function node?

The switch node allows me to match the following regex in a string:
^\#\?[1-3][0-6]$

I'd like to do this in a function node. I tried:

if (msg.payload.search("^\#\?[1-3][0-6]$") > -1) {
    // blah
    return msg;
}

But it doesn't work, i.e. the match is clearly not found as msg is not returned.

What's wrong here?

(Why do I want to do this in a function node? I have a few different matches I want to perform, with different code blocks for each one, and I want to see it clearly on one page which match applies to which code block without having to switch back and forth between the switch node and the function node)

hello .. you need slashes wrapping the regex

[{"id":"03084b6a8123d65d","type":"function","z":"5847b7aa62131d37","name":"","func":"\nmsg.payload = msg.payload.match(/^\\#\\?[1-3][0-6]$/)\n\nif (msg.payload) {\n    node.warn(msg.payload[0]);   // first match\n    return msg;\n}\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":1560,"wires":[["10663b0e50f5fd9e"]]},{"id":"eaafbd92ddb9c638","type":"inject","z":"5847b7aa62131d37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"#?25","payloadType":"str","x":390,"y":1480,"wires":[["03084b6a8123d65d"]]},{"id":"10663b0e50f5fd9e","type":"debug","z":"5847b7aa62131d37","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":1560,"wires":[]},{"id":"d8b0fe4ae2d41355","type":"inject","z":"5847b7aa62131d37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"#25","payloadType":"str","x":390,"y":1540,"wires":[["03084b6a8123d65d"]]},{"id":"100f24e3319532a9","type":"inject","z":"5847b7aa62131d37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"#?55","payloadType":"str","x":390,"y":1600,"wires":[["03084b6a8123d65d"]]},{"id":"257a5fb774798e73","type":"inject","z":"5847b7aa62131d37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"#?36","payloadType":"str","x":390,"y":1660,"wires":[["03084b6a8123d65d"]]}]

Best use /pattern/ in javascript then you can use modifiers etc

Your regex starts with ^, which means start of string. $ at end means end of string.
So unless your string starts with # and ends with 0-6, it will always return -1.
Try removing the $ and ^.

Thanks - that solves it!

Yep, the regex was correct (message always starts with # and ends with 0-6 in this case...) - I just didn't use the forward slashes correctly! Thanks

The use of search() threw me off as i thought you were looking for an index of the first match. Would it not be better to use test() as it returns true or false?

I didn't know about that, looks like that may technically be better, although for this purpose it does pretty much the same thing I think?

if (msg.payload.test(/^\#\?[1-3][0-6]$/) ) {
    // blah
    return msg;
}

But cleaner in the code.

1 Like

i think for test() the regex has to first

if ( /^\#\?[1-3][0-6]$/.test(msg.payload) ) {
   
 return msg;
}

I have another question now, similar topic!

I have incoming message like this:

#>1101102582759203918490

It consists of:
# then > then [1-3] then [1-6] then any 20 digits - in that exact format.

I have regex which works in an online regex tester:
^\#\>[1-3][1-6]\d{20}$

But when I do this in Node-RED function it doesn't work:

if (msg.payload.search(/^\#\>[1-3][1-6]\d{20}$/) != -1) {
    // blah
}

This is strange, because my first two regex matches worked in the same way. This works ok for the message #?15:

if (msg.payload.search(/^\#\?[1-3][0-6]$/) != -1) {
    // blah
}
[{"id":"03084b6a8123d65d","type":"function","z":"5847b7aa62131d37","name":"","func":"\nif (/^\\#\\>[1-3][1-6]\\d{20}$/.test(msg.payload)) {\n    \n    return msg;\n}\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":1620,"wires":[["10663b0e50f5fd9e"]]},{"id":"10663b0e50f5fd9e","type":"debug","z":"5847b7aa62131d37","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":1620,"wires":[]},{"id":"e577b0b8725cf17c","type":"inject","z":"5847b7aa62131d37","name":"#>1101102582759203918490","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"#>1101102582759203918490","payloadType":"str","x":340,"y":1600,"wires":[["03084b6a8123d65d"]]},{"id":"5bd01e16d7f98336","type":"inject","z":"5847b7aa62131d37","name":"#>11011025827","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"#>11011025827","payloadType":"str","x":360,"y":1660,"wires":[["03084b6a8123d65d"]]}]

Ahh - found the problem. My device was returning an additional space at the end :slight_smile: Thanks

1 Like

i was wondering because i tested with search() syntax also and it was working for me :wink:

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