Extract a part of a string

Hi all,

I have a use case that might be very easy for some of you.

I have some codes send in Node Red as String that looks like this :
A0000013683036

I would like to extract only the part 13683036.

If my code sent is A0000213670036, I would like to extract only 213670036.

Do you know how to do it without using a function and javascript ?

Thanks a lot,
Paul

A few variables not specified.
Do you want a string returned?
Does it always start with A?
Try

msg.payload = parseInt(msg.payload.slice(1)).toString();
return msg;

If you require a number returned remove the .toString()

Could possibly be done without JS using change node and a regex or string node or maybe a convoluted flow, but I think JS is possible the cleanest here.

Regex using change node

[{"id":"9fc7d95eb98040b7","type":"inject","z":"613df62afc8a16bf","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"A0000013683036","payloadType":"str","x":130,"y":100,"wires":[["2d1c1afc9cf699dd"]]},{"id":"2d1c1afc9cf699dd","type":"change","z":"613df62afc8a16bf","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"^[A-Z]0*","fromt":"re","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":100,"wires":[["bec5d3f7908b3aab"]]},{"id":"bec5d3f7908b3aab","type":"debug","z":"613df62afc8a16bf","name":"debug 2575","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":140,"wires":[]}]
1 Like

Without using Javascript, but using Jsonata, which is much, much worse:

[{"id":"e4533461c002f3f9","type":"inject","z":"b263e6b23d8eb759","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"A00000426","payloadType":"str","x":110,"y":160,"wires":[["5f83da38286a1f62"]]},{"id":"30a822f24f791137","type":"debug","z":"b263e6b23d8eb759","name":"debug 143","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":160,"wires":[]},{"id":"5f83da38286a1f62","type":"change","z":"b263e6b23d8eb759","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"[A-Z]*[a-z]*","fromt":"re","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":160,"wires":[["7a9e7bcafc4c5611"]]},{"id":"7a9e7bcafc4c5611","type":"change","z":"b263e6b23d8eb759","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":160,"wires":[["30a822f24f791137"]]}]
1 Like

Well for those who like regex, this JSONata expression in a change node is pretty readable, imo... ;*)

payload.$match(/[A-Z]0*(\d+)/).groups[0].$number()

1 Like

Hi all,

Thanks a lot for your inputs ! I tested them all they are perfect !

If you want to use JSONata the this may be cleaner

$number($substring($$.payload, 1))

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