# Regex and numbers

**URL:** <https://discourse.nodered.org/t/regex-and-numbers/64641>\
**Category:** General\
**Created:** [3 July 2022 17:20 UTC](https://discourse.nodered.org/t/regex-and-numbers/64641 "2022-07-03T17:20:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Inoxx](https://avatars.discourse-cdn.com/v4/letter/i/838e76/32.png) [@Inoxx](https://discourse.nodered.org/u/Inoxx)\
**Post date:** [3 July 2022 17:20 UTC](https://discourse.nodered.org/t/regex-and-numbers/64641/1 "2022-07-03T17:20:24Z")

</div>

Hey Node-Red forum,

I'm new here even thou i have been using node-red for some time now, i have never come across a specific issue that didn't have a clear answer to in this forum. But now I'm facing an small issue I have yet to figure out or find any solution for.

So the msg.payload i send is an Array of objects that each object includes a number of an old code. This code I want to translate and re-send as a string in a new Array Object payload with the updated match. And preferable only the update code number.  
But at some points I got a reply that includes several types of replies and at some cases I get nothing in the returned answer from the regex expression.

What am I doing wrong here?

 ![bild](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/1/a1c056500ae22a02691e615740f56ccdd52a4e55.png)

```auto
[{"id":"69e480a0e348fc09","type":"tab","label":"first","disabled":false,"info":"","env":[]},{"id":"b0899b0f5fceb2cd","type":"inject","z":"69e480a0e348fc09","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":210,"y":340,"wires":[["268049f9463a8378"]]},{"id":"94fcc599bfcf579e","type":"debug","z":"69e480a0e348fc09","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":950,"y":340,"wires":[]},{"id":"13cbc46f94182716","type":"debug","z":"69e480a0e348fc09","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":400,"wires":[]},{"id":"268049f9463a8378","type":"function","z":"69e480a0e348fc09","name":"","func":"var msg1 ={ oldCode: 5 }\nvar msg2 ={ oldCode: 10 } \nvar msg3 ={ oldCode: 45 } \nvar msg4 ={ oldCode: 46 } \nvar msg5 ={ oldCode: 44 } \nvar msg6 ={ oldCode: 24 } \nvar msg7 ={ oldCode: 23 } \nvar msg8 ={ oldCode: 22 } \nvar msg9 ={ oldCode: 16 } \nvar msg10 ={ oldCode: 12 }\nvar msg11 ={ oldCode: 0 }\n\nmsg.payload = new Array ([msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9, msg10, msg11])\n \nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":340,"wires":[["13cbc46f94182716","6d023ca9588e23a7"]]},{"id":"6d023ca9588e23a7","type":"function","z":"69e480a0e348fc09","name":"","func":"var codes = [\n {\n oldCode: '5',\n newCode: '09090909-0909'\n },\n {\n oldCode: '0',\n newCode: '09020200-0000'\n },\n {\n oldCode: '10',\n newCode: '02010100-0000'\n },\n {\n oldCode: '12',\n newCode: '06010200-0000'\n },\n {\n oldCode: '16',\n newCode: '06010500-3312'\n },\n {\n oldCode: '22',\n newCode: '05010100-0000'\n },\n {\n oldCode: '23',\n newCode: '05010200-0000'\n },\n {\n oldCode: '24',\n newCode: '05020101-3312'\n },\n {\n oldCode: '44',\n newCode: '05024100-3312'\n },\n {\n oldCode: '45',\n newCode: '05024200-3312'\n },\n {\n oldCode: '46',\n newCode: '05024300-3312'\n }\n \n];\n\nvar result = JSON.stringify(msg.payload);\ncodes.forEach(e => {\n var numberPattern = /(\\\\d)+(\\\\.)/g\n var oldones = e.oldCode.match(numberPattern);\n var searchRegExp = new RegExp(e.oldCode, ''); \n result = result.replace(searchRegExp, e.newCode);\n})\nvar newCode = result.split(\",\",)\nmsg.payload = ([newCode])\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":340,"wires":[["94fcc599bfcf579e"]]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [3 July 2022 18:33 UTC](https://discourse.nodered.org/t/regex-and-numbers/64641/2 "2022-07-03T18:33:43Z")

</div>

I would not use regex for this, i would probably use a lookup object something like this.

```auto
[{"id":"b0899b0f5fceb2cd","type":"inject","z":"69e480a0e348fc09","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"oldCode\":5},{\"oldCode\":10},{\"oldCode\":45},{\"oldCode\":46},{\"oldCode\":44},{\"oldCode\":24},{\"oldCode\":23},{\"oldCode\":22},{\"oldCode\":16},{\"oldCode\":12},{\"oldCode\":0}]","payloadType":"json","x":450,"y":340,"wires":[["6d023ca9588e23a7","13cbc46f94182716"]]},{"id":"6d023ca9588e23a7","type":"function","z":"69e480a0e348fc09","name":"","func":"var code_lookup = {\n '5': '09090909-0909',\n '0': '09020200-0000',\n '10': '02010100-0000',\n '12': '06010200-0000',\n '16': '06010500-3312',\n '22': '05010100-0000',\n '23': '05010200-0000',\n '24': '05020101-3312',\n '44': '05024100-3312',\n '45': '05024200-3312',\n '46': '05024300-3312'\n}\n \n\nconst output = [];\nconst code_keys = Object.keys(code_lookup);\nmsg.payload.forEach(obj => {\n const lookup_key = obj.oldCode.toString();\n if(code_keys.includes(lookup_key)){\n output.push(`{\"oldCode\":\"${code_lookup[lookup_key]}\"}`)\n } \n})\n\nmsg.payload = output;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":670,"y":340,"wires":[["94fcc599bfcf579e"]]},{"id":"13cbc46f94182716","type":"debug","z":"69e480a0e348fc09","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":400,"wires":[]},{"id":"94fcc599bfcf579e","type":"debug","z":"69e480a0e348fc09","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":950,"y":340,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Inoxx](https://avatars.discourse-cdn.com/v4/letter/i/838e76/32.png) [@Inoxx](https://discourse.nodered.org/u/Inoxx)\
**Post date:** [3 July 2022 18:44 UTC](https://discourse.nodered.org/t/regex-and-numbers/64641/3 "2022-07-03T18:44:24Z")

</div>

Thank you, that seems to be a great solution and an somewhat easier one to understand 🙂

---

<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:** [17 July 2022 18:44 UTC](https://discourse.nodered.org/t/regex-and-numbers/64641/4 "2022-07-17T18:44:27Z")

</div>

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