# Regex Function Question

**URL:** https://discourse.nodered.org/t/regex-function-question/50137
**Category:** General
**Created:** [24 August 2021 22:28 UTC](https://discourse.nodered.org/t/regex-function-question/50137 "2021-08-24T22:28:35Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Mick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mick/32/80990_2.png) [@Mick](https://discourse.nodered.org/u/Mick)
#### Post date: [24 August 2021 22:28 UTC](https://discourse.nodered.org/t/regex-function-question/50137/1 "2021-08-24T22:28:35Z")

</div>

@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

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

```

---

<div class="post-metadata">

### Author: ![Mick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mick/32/80990_2.png) [@Mick](https://discourse.nodered.org/u/Mick)
#### Post date: [25 August 2021 01:45 UTC](https://discourse.nodered.org/t/regex-function-question/50137/2 "2021-08-25T01:45:10Z")

</div>

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/b/0b57fe316666ab0ae128a54449bda0f94411d3ca.png)

Regards

---

<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: [8 September 2021 01:45 UTC](https://discourse.nodered.org/t/regex-function-question/50137/3 "2021-09-08T01:45:47Z")

</div>

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