Regex Function Question

@Steve-Mcl Steve, I am trying to modify the regex func you gave me to do a multiple pass and look for the 1st key word ('client' and then set the 2nd regex pass to use that. That way I don't have to make multiple funcs with the same code. Below is sample data like I sent you with my attempt at modifying the code to replace the key word with what I parse out. I can see the 1st parse variable but when I try to insert it in the 2nd regex it is coming back with nulls. Thanks for your time.

Regards,
Mick

[{"id":"2d65ecf9ba85120d","type":"inject","z":"4512ac95.5bc124","name":"clientData","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[83,53,56,56,55,48,66,52,68,124,99,108,105,101,110,116,32,48,120,53,56,56,55,48,66,52,68,32,99,111,110,110,101,99,116,101,100,32,108,111,99,97,108,95,112,116,116,61,49,32,99,108,105,101,110,116,95,105,100,61,49,69,48,50,65,48,70,57,45,51,70,54,65,45,52,54,57,57,45,66,65,54,53,45,70,69,69,50,68,68,67,48,49,56,52,70,32,112,114,111,103,114,97,109,61,83,109,97,114,116,83,68,82,45,87,105,110,13,10]","payloadType":"bin","x":880,"y":560,"wires":[["873b8d31a2d28fc6"]]},{"id":"873b8d31a2d28fc6","type":"function","z":"4512ac95.5bc124","name":"","func":"const str = msg.payload;\nconst regex0 = /\\|(\\w+)/g\n\nlet m;\nm = regex0.exec(str)\nconst result = {\n    a: Number(m[1])\n}\nvar type = m[1]\nlet regex1 = /(\\w+)\\| (\\d+)(.*)/g\nregex1 = new RegExp(`(\\w+)\\|${type} (\\d+)(.*)`,\"g\")\n\nlet v;\nv = regex1.exec(str)\nconst result1 = {\n    type: Number(v[2])\n}\nconst keyvalues = v[3];\n\nconst regex2 = /(?<key>[^=]+)=(?<value>[^ ]+)/g;\n\n\nwhile ((v = regex2.exec(keyvalues)) !== null) {\n    // This is necessary to avoid infinite loops with zero-width matches\n    if (v.index === regex2.lastIndex) {\n        regex2.lastIndex++;\n    }\n    if(v && v.length == 3) {\n        let key = v[1].trim();\n        let val = v[2].trim();\n        result1[key] = val;\n    }\n}\n\nmsg.payload = result1;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1040,"y":560,"wires":[["ee138365d6caa007"]]},{"id":"ee138365d6caa007","type":"debug","z":"4512ac95.5bc124","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1250,"y":560,"wires":[]}]

@Steve-Mcl I figured it out. Thanks again for your help yesterday.

image

Regards

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