# Change Node Regex

**URL:** <https://discourse.nodered.org/t/change-node-regex/56671>\
**Category:** General\
**Created:** [14 January 2022 19:51 UTC](https://discourse.nodered.org/t/change-node-regex/56671 "2022-01-14T19:51:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hobbes1069](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hobbes1069/32/39892_2.png) [@hobbes1069](https://discourse.nodered.org/u/hobbes1069)\
**Post date:** [14 January 2022 19:51 UTC](https://discourse.nodered.org/t/change-node-regex/56671/1 "2022-01-14T19:51:12Z")

</div>

So I read through a few posts but none of them seemed to be exactly what I was looking for. I know I could do this with a function node but was trying to keep things as "no-code" as possible.

I have a string `OKUMA.ASSET.3439.123456` and I'd like to extract the 4 digit number.

I have a Change node with regex set to `\.[0-9]+\.`. Ultimately I want to return just the number, but baby steps. Right now I just want it to return the match but in the "Replace with" inserting $1 is taken literally.

---

<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:** [14 January 2022 20:03 UTC](https://discourse.nodered.org/t/change-node-regex/56671/2 "2022-01-14T20:03:27Z")

</div>

You need to use capture groups ( ) to use $1  
eg.

```auto
$replace($$.payload, /(\.\d+\.)/, "test$1")

```

To return just the 4 digit number i would split on "." and select element [2]

---

<div class="post-metadata">

**Author:** ![hobbes1069](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hobbes1069/32/39892_2.png) [@hobbes1069](https://discourse.nodered.org/u/hobbes1069)\
**Post date:** [14 January 2022 20:25 UTC](https://discourse.nodered.org/t/change-node-regex/56671/3 "2022-01-14T20:25:42Z")

</div>

I have the following and it doesn't quite seem to work. I tried testing just using "payload" in the JSONata editor and it did work, so is there something I need to do with the path? I copied it from the payload.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/e/5eb355f29da0ae6b1efd439331ef69768ec07986.png)

---

<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:** [14 January 2022 20:47 UTC](https://discourse.nodered.org/t/change-node-regex/56671/4 "2022-01-14T20:47:35Z")

</div>

Please provide an example flow with the payload in an the inject node. so i can see the data. I can not do much with a screenshot as i have no data to see if your path is correct.  
something like below

```auto
[{"id":"d17a881.81b99f8","type":"inject","z":"c791cbc0.84f648","name":"The data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"topic\":\"location\",\"payload\":1},{\"topic\":\"location\",\"payload\":\"ddd.fff.3456.33333\"},{\"topic\":\"cockpit\",\"payload\":1},{\"topic\":\"cockpit\",\"payload\":2},{\"topic\":\"battery_status\",\"payload\":1},{\"topic\":\"battery_status\",\"payload\":2}]","payloadType":"json","x":190,"y":840,"wires":[["39f9ee50.cd87aa"]]},{"id":"39f9ee50.cd87aa","type":"change","z":"c791cbc0.84f648","name":"the exprression","rules":[{"t":"set","p":"payload","pt":"msg","to":"$split($$.payload[1].payload, \".\")[2]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":880,"wires":[["c4f45a89.bcb1d8"]]},{"id":"c4f45a89.bcb1d8","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":840,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [14 January 2022 22:14 UTC](https://discourse.nodered.org/t/change-node-regex/56671/5 "2022-01-14T22:14:29Z")

</div>

> [@hobbes1069](#):
>
> OKUMA.ASSET.3439.123456

To extract the 4-digit number with regex , you can use the following Change Node:

```auto
[{"id":"4455bdb3430ed352","type":"change","z":"ce8d30fe991b178a","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":".*\\.(\\d{4})\\..*","fromt":"re","to":"$1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":480,"wires":[["fc4ac1c568fbc98f"]]},{"id":"fc4ac1c568fbc98f","type":"debug","z":"ce8d30fe991b178a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":970,"y":480,"wires":[]},{"id":"354959880a204d2d","type":"inject","z":"ce8d30fe991b178a","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"OKUMA.ASSET.3439.123456","payloadType":"str","x":440,"y":480,"wires":[["4455bdb3430ed352"]]}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/9/99554534ed539fb421d83fab12f1492637954e59.png)

---

<div class="post-metadata">

**Author:** ![hobbes1069](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hobbes1069/32/39892_2.png) [@hobbes1069](https://discourse.nodered.org/u/hobbes1069)\
**Post date:** [14 January 2022 22:14 UTC](https://discourse.nodered.org/t/change-node-regex/56671/6 "2022-01-14T22:14:35Z")

</div>

IT is doing security updates on my RHEL server I run this from so I'll have to put that together Monday. Thanks for the help so far!

---

<div class="post-metadata">

**Author:** ![hobbes1069](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hobbes1069/32/39892_2.png) [@hobbes1069](https://discourse.nodered.org/u/hobbes1069)\
**Post date:** [24 January 2022 19:05 UTC](https://discourse.nodered.org/t/change-node-regex/56671/7 "2022-01-24T19:05:28Z")

</div>

Thanks @mickym2 that got it!

---

<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:** [25 March 2022 19:06 UTC](https://discourse.nodered.org/t/change-node-regex/56671/8 "2022-03-25T19:06:00Z")

</div>

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